Ocelot Swagger Mmlib.swaggerforocelot显示“规格中定义的无操作!”

发布于 2025-01-30 11:40:36 字数 1222 浏览 3 评论 0原文

我正在使用ocelot网关,并使用“ mmlib.swaggerforocelot ”库来进行摇摇欲坠的文档。 对于一些Swagger键,Swagger UI显示“规格中没有定义的操作!” Swagger Json即将到来,没有诸如

{
  "openapi": "3.0.1",
  "info": {
    "title": "Admin API",
    "version": "v1"
  },
  "paths": {},
  "components": {
    "schemas": {}
  }
}

Ocelot配置路线之类的

{
      "DownstreamPathTemplate": "/api/admin/v{version}/{everything} ",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5000
        }
      ],
      "UpstreamPathTemplate": "/api/admin/v{version}/{everything}",
      "UpstreamHttpMethod": [],
      "QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 3,
        "DurationOfBreak": 1000,
        "TimeoutValue": 900000
      },
      "SwaggerKey": "AdminAPI"
    }

路径,而Swagger配置

 "SwaggerEndPoints": [
    {
      "Key": "AdminAPI",
      "Config": [
        {
          "Name": "Admin API",
          "Version": "v1",
          "Url": "http://localhost:5000/swagger/v1/swagger.json"
        }
      ]
    }
    
  ]

在审查 mmlib.swaggerforocelot 源代码之后,它看起来与下游路径中的版本有关这可以解决吗?

I am using Ocelot gateway and for swagger document using "MMLib.SwaggerForOcelot" library.
For some swagger Key, swagger UI is showing "No operations defined in spec!" and swagger JSON is coming without paths like

{
  "openapi": "3.0.1",
  "info": {
    "title": "Admin API",
    "version": "v1"
  },
  "paths": {},
  "components": {
    "schemas": {}
  }
}

Ocelot Configuration Route is

{
      "DownstreamPathTemplate": "/api/admin/v{version}/{everything} ",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5000
        }
      ],
      "UpstreamPathTemplate": "/api/admin/v{version}/{everything}",
      "UpstreamHttpMethod": [],
      "QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 3,
        "DurationOfBreak": 1000,
        "TimeoutValue": 900000
      },
      "SwaggerKey": "AdminAPI"
    }

and Swagger Configuration is

 "SwaggerEndPoints": [
    {
      "Key": "AdminAPI",
      "Config": [
        {
          "Name": "Admin API",
          "Version": "v1",
          "Url": "http://localhost:5000/swagger/v1/swagger.json"
        }
      ]
    }
    
  ]

after reviewing the MMLib.SwaggerForOcelot source code, it looks like something to do with the version in the downstream path, any clue on how this can be fixed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有你 2025-02-06 11:40:36

问题是 mmlib.swaggerforocelot 在执行Ocelot转换时没有考虑{version}。

RouteOptions 具有属性 transformbyocelotconfig 默认设置为TRUE,因此一旦从下游获得Swagger JSON,将进行转换。
在这里,它将尝试从下面的路线配置找到路由,如果找不到的话,它将从Swagger JSON删除路线,

private static RouteOptions FindRoute(IEnumerable<RouteOptions> routes, string downstreamPath, string basePath)
        {
            string downstreamPathWithBasePath = PathHelper.BuildPath(basePath, downstreamPath);
            return routes.FirstOrDefault(p
                => p.CanCatchAll
                    ? downstreamPathWithBasePath.StartsWith(p.DownstreamPathWithSlash, StringComparison.CurrentCultureIgnoreCase)
                    : p.DownstreamPathWithSlash.Equals(downstreamPathWithBasePath, StringComparison.CurrentCultureIgnoreCase));
        }

问题是 startswith 将返回false,因为Swagger JSON路径会

/api/admin/v{version}/Connections

和路线config就是这样

/api/admin/v{version}/{everything}

,并且版本将替换为Swagger选项中给出的版本,以便将其解决

/api/admin/v1/{everything}

此问题的解决方案将

  1. 设置为“ transformbyocelotconfig”:swagger in swagger选项
"SwaggerEndPoints": [
    {
      "Key": "AdminAPI",
      "TransformByOcelotConfig":false,
      "Config": [
        {
          "Name": "Admin API",
          "Version": "v1",
          "Url": "http://localhost:5000/swagger/v1/swagger.json"
        }
      ]
    }
    
  ]
  1. 或更改路由,只是拥有{questhts}键word
{
      "DownstreamPathTemplate": "/api/admin/{everything} ",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5000
        }
      ],
      "UpstreamPathTemplate": "/api/admin/{everything}",
      "UpstreamHttpMethod": [],
      "QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 3,
        "DurationOfBreak": 1000,
        "TimeoutValue": 900000
      },
      "SwaggerKey": "AdminAPI"
    }

The issue is that MMLib.SwaggerForOcelot is not considering {version} while doing Ocelot transformation.

RouteOptions has a property TransformByOcelotConfig which is set to true by default, so once swagger JSON is obtained from downstream, the transformation will be done.
here, it will try to find the route from the route configuration like below and if not found it will delete the route from swagger JSON

private static RouteOptions FindRoute(IEnumerable<RouteOptions> routes, string downstreamPath, string basePath)
        {
            string downstreamPathWithBasePath = PathHelper.BuildPath(basePath, downstreamPath);
            return routes.FirstOrDefault(p
                => p.CanCatchAll
                    ? downstreamPathWithBasePath.StartsWith(p.DownstreamPathWithSlash, StringComparison.CurrentCultureIgnoreCase)
                    : p.DownstreamPathWithSlash.Equals(downstreamPathWithBasePath, StringComparison.CurrentCultureIgnoreCase));
        }

The problem is StartsWith will return false since swagger JSON path will be like

/api/admin/v{version}/Connections

and route config is like

/api/admin/v{version}/{everything}

and version will replace with the version given in swagger options so that it will become

/api/admin/v1/{everything}

Fix to this problem will be

  1. Either set "TransformByOcelotConfig":false in swagger option
"SwaggerEndPoints": [
    {
      "Key": "AdminAPI",
      "TransformByOcelotConfig":false,
      "Config": [
        {
          "Name": "Admin API",
          "Version": "v1",
          "Url": "http://localhost:5000/swagger/v1/swagger.json"
        }
      ]
    }
    
  ]
  1. Or Change the route, just to have {everything} keyword
{
      "DownstreamPathTemplate": "/api/admin/{everything} ",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5000
        }
      ],
      "UpstreamPathTemplate": "/api/admin/{everything}",
      "UpstreamHttpMethod": [],
      "QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 3,
        "DurationOfBreak": 1000,
        "TimeoutValue": 900000
      },
      "SwaggerKey": "AdminAPI"
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文