Fix OpenAPI server URLs for Aspire scenarios#60673
Merged
wtgodbe merged 3 commits intorelease/9.0from Mar 10, 2025
Merged
Conversation
added 2 commits
February 28, 2025 22:40
* Support resolving OpenAPI server URLs from HttpRequest * Try passing optional params everywhere
Contributor
There was a problem hiding this comment.
PR Overview
This PR fixes the generation of OpenAPI server URLs in proxy scenarios by properly using X-Forwarded-Proto and X-Forwarded-Host headers to compute the externally accessible URLs.
- Adds new tests to validate behavior with different forwarded header values.
- Updates the OpenApiDocumentService API to accept an optional HttpRequest parameter and adjust server URL construction accordingly.
- Modifies the endpoint extension to propagate the HttpRequest to the document service.
Reviewed Changes
| File | Description |
|---|---|
| src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiDocumentService/OpenApiDocumentServiceTests.Servers.cs | Introduces tests validating the use of forwarded headers and expected URL generation. |
| src/OpenApi/src/Services/OpenApiDocumentService.cs | Updates method signatures and logic to use the optional HttpRequest for URL generation. |
| src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiDocumentServiceTestsBase.cs | Updates verification calls to pass null for the httpRequest parameter where not applicable. |
| src/OpenApi/src/Extensions/OpenApiEndpointRouteBuilderExtensions.cs | Adjusts method calls to pass the HttpRequest in order to leverage the new URL generation logic. |
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
...NetCore.OpenApi.Tests/Services/OpenApiDocumentService/OpenApiDocumentServiceTests.Servers.cs
Show resolved
Hide resolved
This reverts commit bf991f6.
BrennanConroy
approved these changes
Mar 7, 2025
Member
|
Approved over email |
This was referenced Jul 21, 2025
Closed
This was referenced Jan 8, 2026
Closed
Closed
This was referenced Jan 17, 2026
Open
This was referenced Jan 30, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR supports respecting he X-Forwarded-Proto and X-Forwarded-Host headers when generating server URLs in OpenAPI documents. When these headers are present in the request, the OpenAPI document service will use them to generate the correct server URLs instead of using the original host and scheme values derived from the service configuration.
This is particularly useful in environments where the API is behind a proxy, load balancer, or gateway, allowing the generated OpenAPI document to correctly reference the public-facing URL rather than the internal service URL.
Fixes #57332
Customer Impact
Without this change, documents served behind reverse proxies or forwarded endpoints do not reflect the correct service URl, particularly impact for the ASP.NET Core + Aspire scenario. While the issue is easy to workaround, we want a smoother experience with Aspire out-of-the-box.
Regression?
Risk
Low-risk, becase change as it only affects the generation of server URLs in OpenAPI documents and does not impact the actual API functionality.
Verification
Packaging changes reviewed?