Skip to content

Fix handling for inert route parameters in MVC #58246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ private async Task<OpenApiResponse> GetResponseAsync(
var targetType = parameter.Type == typeof(string) && parameter.ModelMetadata.ModelType != parameter.Type
? parameter.ModelMetadata.ModelType
: parameter.Type;
// If the type is null, then we're dealing with an inert
// route parameter that does not define a specific parameter type in the
// route handler or in the response. In this case, we default to a string schema.
targetType ??= typeof(string);
var openApiParameter = new OpenApiParameter
{
Name = parameter.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,22 @@ internal enum ItemStatus
Approved = 1,
Rejected = 2,
}

[Fact]
public async Task SupportsMvcActionWithAmbientRouteParameter()
{
// Arrange
var action = CreateActionDescriptor(nameof(AmbientRouteParameter));

// Assert
await VerifyOpenApiDocument(action, document =>
{
var operation = document.Paths["/api/with-ambient-route-param/{versionId}"].Operations[OperationType.Get];
var parameter = Assert.Single(operation.Parameters);
Assert.Equal("string", parameter.Schema.Type);
});
}

[Route("/api/with-ambient-route-param/{versionId}")]
private void AmbientRouteParameter() { }
}
Loading