Fix OpenAPI generator crash on routes starting with '~'#64408
Conversation
… 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() |
There was a problem hiding this comment.
@copilot Add a test that validates the behavior of these types of paths from a controller action.
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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
RoutePatternproperty toApiDescriptionclass to cache the parsed route pattern - Updated both API description providers (
EndpointMetadataApiDescriptionProviderandDefaultApiDescriptionProvider) to populate the new property - Modified
MapRelativePathToItemPathto 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 '~' |
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Extensions/ApiDescriptionExtensionsTests.cs
Show resolved
Hide resolved
...spNetCore.OpenApi.Tests/Services/OpenApiDocumentService/OpenApiDocumentServiceTests.Paths.cs
Show resolved
Hide resolved
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Extensions/ApiDescriptionExtensionsTests.cs
Show resolved
Hide resolved
...spNetCore.OpenApi.Tests/Services/OpenApiDocumentService/OpenApiDocumentServiceTests.Paths.cs
Show resolved
Hide resolved
Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
Fix OpenAPI generator crash on routes starting with '~'
Store RoutePattern on ApiDescription to avoid reparsing
Description
MapRelativePathToItemPathwas reparsingRelativePathwithRoutePatternFactory.Parse(), which fails for routes starting with~since the stored path lacks the leading/(e.g.,~healthvs/~health).Solution: Add
RoutePatternproperty toApiDescriptionand populate it in both providers:EndpointMetadataApiDescriptionProviderusesrouteEndpoint.RoutePatternDefaultApiDescriptionProviderparses fromAttributeRouteInfo.Template(simplified assignment based on code review)MapRelativePathToItemPathuses stored pattern, falls back to parsing for compatibilityExample:
Changes:
RoutePatternproperty toApiDescriptionMapRelativePathToItemPathto use pre-parsed patternDefaultApiDescriptionProviderTests:
~(covering both/~healthand~/healthpatterns)~route patterns~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.