-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Remove trailing slash from OpenAPI server URLs when pathBase is empty #64716
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
Conversation
…empty Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR removes the trailing slash from OpenAPI server URLs when the path base is empty, aligning the generated OpenAPI documents with the OpenAPI 3.1.0 specification examples. The change ensures that server URLs like https://example.com/ are correctly formatted as https://example.com when no explicit path base is set.
Key Changes:
- Modified
GetOpenApiServers()to strip trailing slashes whenpathBase.HasValueis false - Preserved trailing slashes when
pathBaseexplicitly contains "/" to maintain intentional path structure - Updated test expectations and snapshot files to reflect the corrected URL format
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/OpenApi/src/Services/OpenApiDocumentService.cs | Added logic to remove trailing slash from server URLs when pathBase is empty |
| src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiDocumentService/OpenApiDocumentServiceTests.Servers.cs | Updated test expectations and added new test case to verify trailing slash removal |
| src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApiDocumentLocalizationTests.VerifyOpenApiDocumentIsInvariant.verified.txt | Updated snapshot to reflect server URL without trailing slash |
| // Keep the trailing slash if pathBase explicitly contains "/" to preserve intentional path structure. | ||
| if (serverUrl.EndsWith('/') && !httpRequest.PathBase.HasValue) | ||
| { | ||
| serverUrl = serverUrl.TrimEnd('/'); |
Copilot
AI
Dec 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TrimEnd('/') method will remove all trailing slashes, not just one. Since UriHelper.BuildAbsolute() only appends a single trailing slash, consider using serverUrl[..^1] for more efficient string manipulation that removes exactly one character.
| serverUrl = serverUrl.TrimEnd('/'); | |
| serverUrl = serverUrl[..^1]; |
| // Assert | ||
| Assert.Single(servers); | ||
| Assert.Equal("https://example.com", servers[0].Url); | ||
| Assert.DoesNotContain("https://example.com/", servers.Select(s => s.Url)); |
Copilot
AI
Dec 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertion on line 181 is redundant since line 180 already verifies that the URL equals https://example.com exactly. If servers[0].Url equals https://example.com, it cannot also be https://example.com/. Consider removing line 181 to simplify the test.
| Assert.DoesNotContain("https://example.com/", servers.Select(s => s.Url)); |
Remove trailing slash from OpenAPI server URLs when pathBase is empty
Remove trailing slash from server URLs with empty pathBase per OpenAPI spec
Description
UriHelper.BuildAbsolute()appends a trailing slash when bothpathBaseandpathare empty. This causes generated OpenAPI documents to containhttps://example.com/instead ofhttps://example.com, diverging from OpenAPI 3.1.0 specification examples.Changes:
GetOpenApiServers()to strip trailing slash whenpathBase.HasValueis falsepathBaseexplicitly contains "/" to maintain intentional path structureBefore:
{ "servers": [ { "url": "https://example.com/" } ] }After:
{ "servers": [ { "url": "https://example.com" } ] }Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.