Skip to content
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

Open
1 task done
martincostello opened this issue Aug 14, 2024 · 10 comments
Open
1 task done
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi
Milestone

Comments

@martincostello
Copy link
Member

martincostello commented Aug 14, 2024

Is there an existing issue for this?

  • I have searched the existing issues

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

@martincostello martincostello added feature-openapi area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Aug 14, 2024
@martincostello martincostello changed the title Server URLs in OpenAPI document are correct when application used with .NET Aspire Server URLs in OpenAPI document are incorrect when application used with .NET Aspire Aug 14, 2024
@captainsafia captainsafia added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Aug 22, 2024
@captainsafia
Copy link
Member

@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.

@ryuuc
Copy link
Contributor

ryuuc commented Nov 13, 2024

The issue still exists in .NET 9.0.100

Image

@xetle
Copy link

xetle commented Nov 15, 2024

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
https://localhost:7163 & http://localhost:5136

but https://localhost:7163/openapi/v1.json gives
"servers": [
{
"url": "https://localhost:49243"
},
{
"url": "http://localhost:49244"
}
],

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
"servers": [
{
"url": "https://localhost:7163"
},
{
"url": "http://localhost:5136"
}
],

@martincostello
Copy link
Member Author

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.

@boulc
Copy link

boulc commented Nov 17, 2024

As another workaround, the servers defined in the OpenAPI document can be overridden to an empty array:

app.MapScalarApiReference(_ => _.Servers = []);

Image

@xetle
Copy link

xetle commented Nov 18, 2024

As another workaround, the servers defined in the OpenAPI document can be overridden to an empty array:

app.MapScalarApiReference(_ => _.Servers = []);

Nice! - that worked - Thanks

@skordesign
Copy link

I deploy to docker and the server URL shown http://[::]:8080, what can I do to change it?
Image

@raky291
Copy link

raky291 commented Nov 27, 2024

I have the same issue, is there still no solution?

I'm using OpenAPI + Swashbuckle.AspNetCore.SwaggerUI

@martincostello
Copy link
Member Author

@raky291 #57332 (comment)

@davidfowl
Copy link
Member

We’re going to need to do something here to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi
Projects
None yet
Development

No branches or pull requests

8 participants