Ocelot Swagger MMLib.SwaggerForOcelot 显示 "No operations defined in spec!"
Ocelot Swagger MMLib.SwaggerForOcelot showing "No operations defined in spec!"
我正在使用 Ocelot 网关并使用“MMLib.SwaggerForOcelot”库来制作 swagger 文档。
对于某些 swagger 键,swagger UI 显示“规范中未定义任何操作!”招摇 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 源代码后,看起来与下游路径中的版本有关,关于如何解决这个问题的任何线索?
问题是 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 会 return false 因为 swagger JSON 路径会像
/api/admin/v{version}/Connections
路由配置类似
/api/admin/v{version}/{everything}
并且版本将替换为 swagger 选项中给出的版本,这样它将变为
/api/admin/v1/{everything}
解决此问题的方法是
- 在 swagger 选项中设置“TransformByOcelotConfig”:false
"SwaggerEndPoints": [
{
"Key": "AdminAPI",
"TransformByOcelotConfig":false,
"Config": [
{
"Name": "Admin API",
"Version": "v1",
"Url": "http://localhost:5000/swagger/v1/swagger.json"
}
]
}
]
- 或更改路线,只为拥有{everything}关键字
{
"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"
}
我正在使用 Ocelot 网关并使用“MMLib.SwaggerForOcelot”库来制作 swagger 文档。 对于某些 swagger 键,swagger UI 显示“规范中未定义任何操作!”招摇 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 源代码后,看起来与下游路径中的版本有关,关于如何解决这个问题的任何线索?
问题是 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 会 return false 因为 swagger JSON 路径会像
/api/admin/v{version}/Connections
路由配置类似
/api/admin/v{version}/{everything}
并且版本将替换为 swagger 选项中给出的版本,这样它将变为
/api/admin/v1/{everything}
解决此问题的方法是
- 在 swagger 选项中设置“TransformByOcelotConfig”:false
"SwaggerEndPoints": [
{
"Key": "AdminAPI",
"TransformByOcelotConfig":false,
"Config": [
{
"Name": "Admin API",
"Version": "v1",
"Url": "http://localhost:5000/swagger/v1/swagger.json"
}
]
}
]
- 或更改路线,只为拥有{everything}关键字
{
"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"
}