[英]Swagger shows "Failed to load API definition" error when accessing from YARP proxy server
Since ocelot is no longer maintained, I decided to give a go to YARP as my API Gateway but it's giving me this silly error when I access one of my endpoints through it:由于不再维护 ocelot,我决定将 go 作为我的 API 网关提供给 YARP,但是当我通过它访问我的端点之一时,它给了我这个愚蠢的错误:
"ReverseProxy": {
"Routes": {
"client-route": {
"ClusterId": "client-cluster",
"CorsPolicy": "customPolicy",
"Match": {
"Path": "client-service/{**remainder}"
},
"Transforms": [
{ "X-Forwarded": "Off" },
{
"PathPattern": "/{**remainder}"
}
]
}
},
"Clusters": {
"client-cluster": {
"Destinations": {
"destination1": {
"Address": "https://URLWithSwagger/"
}
}
}
}
}
when using the configuration above, I get the following error:使用上面的配置时,出现以下错误:
but when I change the path from "client-service/{**remainder}"
to "/{**remainder}"
, everything works fine?但是当我将路径从
"client-service/{**remainder}"
更改为"/{**remainder}"
时,一切正常吗? any suggestions on the root of this error?关于这个错误的根源有什么建议吗?
Update: Here's the program.cs:更新:这是 program.cs:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddReverseProxy()//.AddConfigFilter();
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapReverseProxy();
app.Run();
In your sample, the path client-service/url
is forwarded as is to the backend.在您的示例中,路径
client-service/url
按原样转发到后端。 And such url does not exists.而这样的 url 是不存在的。 That's why removing the prefix works again.
这就是为什么删除前缀再次起作用的原因。
To fix: you need to transform the url to remove the client-service
prefix per route.要修复:您需要转换 url 以删除每个路由的
client-service
前缀。 You can do it with yarp adding the PathRemovePrefix
transformer:您可以通过添加
PathRemovePrefix
转换器的yarp来实现:
"client-route": {
"ClusterId": "client-cluster",
"Match": {
"Path": "/client-service/{****remainder}"
},
"Transforms": [
{ "PathRemovePrefix": "/client-service" }
]
},
This will work to get the raw swagger Specification (JSON file).这将用于获取原始 swagger 规范(JSON 文件)。 If you want to render it on SwaggerUI you should inject SwaggerUI on the reverse-proxy and pass the forwarded contract: openapi.json URI to it.
如果你想在 SwaggerUI 上呈现它,你应该在反向代理上注入 SwaggerUI 并将转发的合同:openapi.json URI 传递给它。
For more details on this one, see this issue .有关这方面的更多详细信息,请参阅此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.