-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Server URLs in OpenAPI document are incorrect when application used with .NET Aspire #57332
Comments
@martincostello The transformer strategy is a good way to solve this problem. A while back with modeled Swagger UI as an Aspire resource (https://github.com/davidfowl/AspireSwaggerUI/blob/146163aec403305770861098d3c16578a0d5dd99/SwaggerUi.Aspire.Hosting/SwaggerUiExtensions.cs) and configured forwarding for requests using Aspire's endpoint forwarding rules. In this approach, the Swagger UI is served outside the API resource instance and mimics a pattern similar to what we would do if we served an API testing UI in Aspire. I'll stick this in the backlog for now as we evaluate how first class OpenAPI docs become in Aspire/Aspire dashboard. In the short-term, we can doc why this issue happens and how to resolve. |
I get this error as well. I downloaded the latest VS 2022 today. If I create a new ASP.NET Core Web API project with .NET 9 and Enlist in .NET Aspire orchestration. In the Aspire dashboard I see these endpoints but https://localhost:7163/openapi/v1.json gives This causes issues with Scalar because I want to call the endpoint https://localhost:7163/scalar/v1 and the TestRequest for WeatherForecast will use 49243. Whereas if I set the API project as my startup project I see this and https://localhost:7163/scalar/v1 works |
As a workaround, you can fix this by using my OpenAPI Extensions library: builder.Services.AddOpenApiExtensions(options => options.AddServerUrls = true); This should take into account the forwarded headers and should map to the correct URLs - code. |
Nice! - that worked - Thanks |
I have the same issue, is there still no solution? I'm using |
We’re going to need to do something here to fix this. |
Is there an existing issue for this?
Describe the bug
When an OpenAPI document is being used as part of an application running with .NET Aspire, the
servers
array of the OpenAPI document is populated with incorrect URLs.This then causes operations using Swagger UI to fail:
In this case, the application is running on
http://localhost:50000
andhttps://localhost:50001
.I think the issue is that headers such as
X-Forwarded-For
aren't being taken into account.aspnetcore/src/OpenApi/src/Services/OpenApiDocumentService.cs
Lines 190 to 198 in 08b60af
If I use my own transformer to always populate the server URLs, which uses the configured
ForwardedHeadersOptions
, then I get the correct host and port.If this is disabled and the built-in development-time support is used, the wrong URLs are rendered.
Expected Behavior
The correct URL(s) are included in the
servers
array of the OpenAPI document taking into account any HTTP forwarded headers.Steps To Reproduce
https://localhost:50001/openapi/api.json
Exceptions (if any)
No response
.NET Version
9.0.100-preview.7.24407.12
Anything else?
No response
The text was updated successfully, but these errors were encountered: