Server URLs in OpenAPI document are incorrect when application used with .NET Aspire · Issue #57332 · dotnet/aspnetcore (original) (raw)

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:

image

In this case, the application is running on http://localhost:50000 and https://localhost:50001.

I think the issue is that headers such as X-Forwarded-For aren't being taken into account.

internal List<OpenApiServer> GetOpenApiServers()
{
if (hostEnvironment.IsDevelopment() &&
server?.Features.Get<IServerAddressesFeature>()?.Addresses is { Count: > 0 } addresses)
{
return addresses.Select(address => new OpenApiServer { Url = address }).ToList();
}
return [];
}

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.

{ "openapi": "3.0.1", "info": { "title": "London Travel", "description": "London Travel is an Amazon Alexa skill for checking the status for travel in London.", "termsOfService": "https://londontravel.martincostello.com/terms-of-service/", "contact": { "name": "Martin Costello", "url": "https://github.com/martincostello/alexa-london-travel-site" }, "license": { "name": "Apache 2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0.html" }, "version": "" }, "servers": [ { "url": "https://localhost:50001" } ] }

If this is disabled and the built-in development-time support is used, the wrong URLs are rendered.

{ "openapi": "3.0.1", "info": { "title": "London Travel", "description": "London Travel is an Amazon Alexa skill for checking the status for travel in London.", "termsOfService": "https://londontravel.martincostello.com/terms-of-service/", "contact": { "name": "Martin Costello", "url": "https://github.com/martincostello/alexa-london-travel-site" }, "license": { "name": "Apache 2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0.html" }, "version": "" }, "servers": [ { "url": "https://localhost:65076" }, { "url": "http://localhost:65077" } ] }

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

  1. Clone martincostello/alexa-london-travel-site@23c2af9
  2. Open the solution in Visual Studio
  3. Launch the LondonTravel.Site.AppHost project
  4. Wait for the applications to load
  5. View the contents of the document at https://localhost:50001/openapi/api.json

Exceptions (if any)

No response

.NET Version

9.0.100-preview.7.24407.12

Anything else?

No response