Skip to content

Commit

Permalink
[release/9.0] Fix handling for inert route parameters in MVC endpoint…
Browse files Browse the repository at this point in the history
…s for OpenAPI (#58311)

* Fix handling for ambient route parameters

* Set default type earlier

---------

Co-authored-by: Safia Abdalla <safia@microsoft.com>
  • Loading branch information
github-actions[bot] and captainsafia authored Oct 14, 2024
1 parent 00eecee commit 5d995b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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() { }
}

0 comments on commit 5d995b6

Please # to comment.