Skip to content

Conversation

martincostello
Copy link
Member

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

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.
@Copilot Copilot AI review requested due to automatic review settings July 29, 2025 11:59
@martincostello martincostello requested review from captainsafia and a team as code owners July 29, 2025 11:59
@github-actions github-actions bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jul 29, 2025
action.AttributeRouteInfo = new()
{
Template = action.MethodInfo.GetCustomAttribute<RouteAttribute>()?.Template,
Template = action.MethodInfo.GetCustomAttribute<RouteAttribute>()?.Template ?? string.Empty,
Copy link
Member Author

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.");

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jul 29, 2025
Copy link
Contributor

@Copilot 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 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));

Comment on lines +505 to +509
if (parameter.ModelMetadata is Mvc.ModelBinding.Metadata.DefaultModelMetadata { Attributes.PropertyAttributes.Count: > 0 } metadata &&
metadata.Attributes.PropertyAttributes.OfType<DescriptionAttribute>().LastOrDefault() is { } propertyDescription)
{
return propertyDescription.Description;
}
Copy link
Member Author

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.

Copy link
Member

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 API
  • DefaultModelMetadata 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.
@captainsafia captainsafia merged commit 9cdd601 into dotnet:main Aug 1, 2025
30 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-rc1 milestone Aug 1, 2025
@martincostello martincostello deleted the gh-61297 branch August 2, 2025 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates community-contribution Indicates that the PR has been added by a community member feature-openapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue with OpenAPI Documentation Generation When Using [FromQuery] in a Separate Class
2 participants