Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 9, 2025

Remove trailing slash from OpenAPI server URLs when pathBase is empty

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Remove trailing slash from server URLs with empty pathBase per OpenAPI spec

Description

UriHelper.BuildAbsolute() appends a trailing slash when both pathBase and path are empty. This causes generated OpenAPI documents to contain https://example.com/ instead of https://example.com, diverging from OpenAPI 3.1.0 specification examples.

Changes:

  • Modified GetOpenApiServers() to strip trailing slash when pathBase.HasValue is false
  • Preserves trailing slash when pathBase explicitly contains "/" to maintain intentional path structure
  • Updated test expectations and snapshot tests to reflect correct behavior

Before:

{
  "servers": [
    { "url": "https://example.com/" }
  ]
}

After:

{
  "servers": [
    { "url": "https://example.com" }
  ]
}
Original prompt

This section details on the original issue you should resolve

<issue_title>[OpenAPI] Trailing slash in servers.url when generating OpenAPI spec</issue_title>
<issue_description>### Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When generating an OpenAPI document in ASP.NET Core, the servers section ends up with a URL that includes a trailing /. This comes from GetOpenApiServers method, which relies on UriHelper.BuildAbsolute(). That helper always appends a trailing slash.

According to the OpenAPI 3.1.0 specification (section 4.8.5.2), the server URLs in the examples is shown without a trailing slash. Because of the current behavior, the generated document doesn’t align with the expected format.

The issue shows up immediately after adding AddOpenApi() with no extra configuration: the generated document contains https://example.com/ instead of https://example.com

Expected Behavior

servers.url should be generated without a trailing slash, matching the example from the specification.

Steps To Reproduce

Steps:

  1. Create a ASP.NET Core project (minimal api / web api).
  2. Register OpenAPI using AddOpenApi().
  3. Open the generated document.
  4. The servers section shows a URL ending with /.

Exceptions (if any)

No response

.NET Version

10.0.100

Anything else?

Microsoft.AspNetCore.OpenApi 10
Visual Studio Insiders 2026 11206.111
OpenAPI v3.1.0, ServerObject - https://spec.openapis.org/oas/v3.1.0.html#server-object</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 9, 2025 19:20
…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>
Copilot AI changed the title [WIP] Fix trailing slash in servers.url when generating OpenAPI spec Remove trailing slash from OpenAPI server URLs when pathBase is empty Dec 9, 2025
Copilot AI requested a review from captainsafia December 9, 2025 19:30
@captainsafia captainsafia marked this pull request as ready for review December 15, 2025 18:32
@captainsafia captainsafia requested a review from a team as a code owner December 15, 2025 18:32
Copilot AI review requested due to automatic review settings December 15, 2025 18:32
Copy link
Contributor

Copilot AI left a 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 when pathBase.HasValue is false
  • Preserved trailing slashes when pathBase explicitly 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('/');
Copy link

Copilot AI Dec 15, 2025

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.

Suggested change
serverUrl = serverUrl.TrimEnd('/');
serverUrl = serverUrl[..^1];

Copilot uses AI. Check for mistakes.
// Assert
Assert.Single(servers);
Assert.Equal("https://example.com", servers[0].Url);
Assert.DoesNotContain("https://example.com/", servers.Select(s => s.Url));
Copy link

Copilot AI Dec 15, 2025

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.

Suggested change
Assert.DoesNotContain("https://example.com/", servers.Select(s => s.Url));

Copilot uses AI. Check for mistakes.
@captainsafia captainsafia enabled auto-merge (squash) December 15, 2025 18:44
@captainsafia captainsafia merged commit 2b34e0b into main Dec 15, 2025
30 checks passed
@captainsafia captainsafia deleted the copilot/fix-trailing-slash-openapi-spec branch December 15, 2025 19:39
@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview1 milestone Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[OpenAPI] Trailing slash in servers.url when generating OpenAPI spec

3 participants