Remove trailing slash from OpenAPI server URLs when pathBase is empty#64716
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.
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('/'); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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.