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

2 participants