Skip to content

Comments

Fix OpenAPI generator crash on routes starting with '~'#64408

Merged
wtgodbe merged 6 commits intomainfrom
copilot/fix-openapi-route-template
Jan 22, 2026
Merged

Fix OpenAPI generator crash on routes starting with '~'#64408
wtgodbe merged 6 commits intomainfrom
copilot/fix-openapi-route-template

Conversation

Copy link
Contributor

Copilot AI commented Nov 17, 2025

Fix OpenAPI generator crash on routes starting with '~'

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

Store RoutePattern on ApiDescription to avoid reparsing

Description

MapRelativePathToItemPath was reparsing RelativePath with RoutePatternFactory.Parse(), which fails for routes starting with ~ since the stored path lacks the leading / (e.g., ~health vs /~health).

Solution: Add RoutePattern property to ApiDescription and populate it in both providers:

  • EndpointMetadataApiDescriptionProvider uses routeEndpoint.RoutePattern
  • DefaultApiDescriptionProvider parses from AttributeRouteInfo.Template (simplified assignment based on code review)
  • MapRelativePathToItemPath uses stored pattern, falls back to parsing for compatibility

Example:

builder.MapGet("/~health", () => Results.Ok("Healthy"));
builder.MapOpenApi(); // Previously crashed, now works

Changes:

  • Added RoutePattern property to ApiDescription
  • Updated both API description providers to set the property
  • Modified MapRelativePathToItemPath to use pre-parsed pattern
  • Added routing assembly reference to Mvc.Abstractions
  • Updated PublicAPI files
  • Simplified RoutePattern assignment logic in DefaultApiDescriptionProvider

Tests:

  • Added 6 unit tests verifying routes starting with ~ (covering both /~health and ~/health patterns)
  • Added integration tests for minimal API endpoints with both ~ route patterns
  • Added integration tests for MVC controller actions with both ~ route patterns (2 tests)

All tests cover both the unusual /~ pattern (tilde after slash) that caused the crash and the normal ~/ pattern (tilde before slash) to ensure comprehensive coverage.


✨ 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 self-assigned this Nov 17, 2025
Copilot AI and others added 2 commits November 17, 2025 23:31
… to use it

Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
}

[Fact]
public async Task GetOpenApiPaths_HandlesRoutesStartingWithTilde()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Add a test that validates the behavior of these types of paths from a controller action.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test GetOpenApiPaths_HandlesRoutesStartingWithTilde_MvcAction that validates routes starting with ~ from MVC controller actions. The test uses a controller action with [Route("/~health")] attribute and verifies the OpenAPI document is generated correctly. Commit: e48e103

Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
@captainsafia captainsafia marked this pull request as ready for review January 16, 2026 00:53
@captainsafia captainsafia requested review from a team and halter73 as code owners January 16, 2026 00:53
Copilot AI review requested due to automatic review settings January 16, 2026 00:53
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 fixes a crash in the OpenAPI generator when handling routes that start with the '~' character by storing the parsed RoutePattern on ApiDescription instead of reparsing the RelativePath string.

Changes:

  • Added RoutePattern property to ApiDescription class to cache the parsed route pattern
  • Updated both API description providers (EndpointMetadataApiDescriptionProvider and DefaultApiDescriptionProvider) to populate the new property
  • Modified MapRelativePathToItemPath to use the cached pattern with fallback to parsing for backward compatibility

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Mvc/Mvc.Abstractions/src/ApiExplorer/ApiDescription.cs Added RoutePattern property with XML documentation
src/Mvc/Mvc.Abstractions/src/Microsoft.AspNetCore.Mvc.Abstractions.csproj Added reference to Microsoft.AspNetCore.Routing assembly
src/Mvc/Mvc.Abstractions/src/PublicAPI.Unshipped.txt Documented public API additions for RoutePattern getter/setter
src/Mvc/Mvc.ApiExplorer/src/PublicAPI.Unshipped.txt Documented forwarded API from Mvc.Abstractions
src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs Set RoutePattern from routeEndpoint.RoutePattern
src/Mvc/Mvc.ApiExplorer/src/DefaultApiDescriptionProvider.cs Parse and set RoutePattern from AttributeRouteInfo.Template
src/OpenApi/src/Extensions/ApiDescriptionExtensions.cs Use cached RoutePattern or fallback to parsing RelativePath
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Extensions/ApiDescriptionExtensionsTests.cs Added unit tests for routes starting with '~'
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiDocumentService/OpenApiDocumentServiceTests.Paths.cs Added integration tests for minimal API and MVC routes with '~'

Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
Copilot AI requested a review from halter73 January 20, 2026 21:41
@captainsafia
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@captainsafia captainsafia disabled auto-merge January 22, 2026 21:36
@wtgodbe wtgodbe merged commit caa56b2 into main Jan 22, 2026
22 of 25 checks passed
@wtgodbe wtgodbe deleted the copilot/fix-openapi-route-template branch January 22, 2026 23:03
@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview1 milestone Jan 22, 2026
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.

5 participants