-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[OpenAPI] Get parameter description with [FromQuery] #62986
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
Conversation
Get the description from the associated object's property when `[FromQuery]` is applied to a property of an object used as a `[FromQuery]` parameter. Resolves dotnet#61297.
action.AttributeRouteInfo = new() | ||
{ | ||
Template = action.MethodInfo.GetCustomAttribute<RouteAttribute>()?.Template, | ||
Template = action.MethodInfo.GetCustomAttribute<RouteAttribute>()?.Template ?? string.Empty, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is to make this assertion happy:
Debug.Assert(apiDescription.RelativePath != null, "Relative path cannot be null."); |
There was a problem hiding this 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 enhances OpenAPI parameter description extraction to support descriptions from properties with [FromQuery]
attributes. When a parameter uses [FromQuery]
on an object property, the OpenAPI generator will now extract the description from the property's [Description]
attribute.
Key changes:
- Enhanced parameter description extraction logic to check model metadata for property-level descriptions
- Added comprehensive test coverage for both
[AsParameters]
and[FromQuery]
scenarios - Fixed a minor issue with route template handling in test infrastructure
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
OpenApiDocumentService.cs | Enhanced GetParameterDescriptionFromAttribute method to extract descriptions from property metadata when parameter-level descriptions are not available |
OpenApiSchemaService.ParameterSchemas.cs | Added test cases for both [AsParameters] and [FromQuery] scenarios with description attributes, including test model definitions |
OpenApiDocumentServiceTestsBase.cs | Fixed potential null reference issue in route template handling for test infrastructure |
Comments suppressed due to low confidence (1)
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.ParameterSchemas.cs:534
- The test creates an action descriptor but doesn't have an 'Act' section. The test should explicitly invoke the OpenAPI generation process to ensure the parameter description extraction is being tested properly.
var actionDescriptor = CreateActionDescriptor(nameof(TestFromQueryController.GetWithFromQueryDto), typeof(TestFromQueryController));
if (parameter.ModelMetadata is Mvc.ModelBinding.Metadata.DefaultModelMetadata { Attributes.PropertyAttributes.Count: > 0 } metadata && | ||
metadata.Attributes.PropertyAttributes.OfType<DescriptionAttribute>().LastOrDefault() is { } propertyDescription) | ||
{ | ||
return propertyDescription.Description; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure this is right, as it feels wrong to depend on a specific model metadata implementation, but I couldn't immediately see an obvious interface-y/abstract way to query the same information and this was "right there" in the debugger.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think for practical cases this is sufficient.
The APIs will only ever end up consuming two metadata types:
EndpointModelMetadata
which is used for minimal APIDefaultModelMetadata
for MVC
The minimal API cases end up getting handled by the IParameterInfoParameterDescriptor
because of the way we map everything, including [AsParameters]
to parameter descriptions via things like PropertyAsParameterInfo
.
This is also why the bug reports that we've received specifically site this gap in controllers and not minimal APIs.
So although it looks werid and is awakward, I think this is the correct way give the fact that MVC relies on model metadata and minimal API doesn't.
Get the default value from the associated object's property when `[FromQuery]` is applied to a property of an object used as a `[FromQuery]` parameter. Resolves dotnet#61934.
Get parameter description with [FromQuery]
Get an OpenAPI description when attribute is on an object's property.
Description
Get the description from the associated object's property when
[FromQuery]
is applied to a property of an object used as a[FromQuery]
parameter.Fixes #61297