Skip to content

Respect IModelNameProvider when matching OpenAPI parameters#64535

Open
khellang wants to merge 19 commits into
dotnet:mainfrom
khellang:fix/openapi-name-provider
Open

Respect IModelNameProvider when matching OpenAPI parameters#64535
khellang wants to merge 19 commits into
dotnet:mainfrom
khellang:fix/openapi-name-provider

Conversation

@khellang

@khellang khellang commented Nov 26, 2025

Copy link
Copy Markdown
Member

Description

This makes sure any attribute implementing IModelNameProvider is respected when matching OpenAPI operation parameters with .NET action parameters.

It also replaces the CancellationToken workaround from the initial review with a proper check against BindingSource.Body, so documentation for parameters that aren't part of the OpenAPI surface (e.g. CancellationToken or [FromServices] parameters) is no longer applied to the request body description.

Fixes #64534
Fixes #64521
Fixes #67076
Fixes #65805

Copilot AI review requested due to automatic review settings November 26, 2025 08:45
@khellang khellang requested review from a team and captainsafia as code owners November 26, 2025 08:45
@github-actions github-actions Bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Nov 26, 2025
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 26, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds support for respecting IModelNameProvider attributes (like FromQuery(Name = "...")) when matching OpenAPI operation parameters with .NET action parameters for XML comment documentation. This ensures that XML documentation comments are correctly applied to parameters that have custom names specified via model binding attributes.

Key Changes:

  • Introduces a new GetOperationParameter helper method that considers both the parameter's original name and any custom names from IModelNameProvider attributes
  • Adds early null check for parameterInfo before attempting to match operation parameters
  • Updates generated code across all snapshot test files to include the new logic

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

File Description
src/OpenApi/gen/XmlCommentGenerator.cs Adds System.Reflection using statement for ParameterInfo type
src/OpenApi/gen/XmlCommentGenerator.Emitter.cs Implements the core logic for parameter matching with IModelNameProvider support; adds GetOperationParameter helper method and includes Microsoft.AspNetCore.Mvc.ModelBinding namespace
src/OpenApi/test/.../snapshots/*.verified.cs Updates all snapshot test files to reflect the generated code changes from the source generator

Comment thread src/OpenApi/gen/XmlCommentGenerator.Emitter.cs Outdated
@martincostello martincostello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Nov 26, 2025
@khellang khellang force-pushed the fix/openapi-name-provider branch 3 times, most recently from 68d5b6f to 16a616e Compare November 26, 2025 09:27
@khellang khellang requested a review from Copilot November 26, 2025 09:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Comment thread src/OpenApi/gen/XmlCommentGenerator.Emitter.cs Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Comment thread src/OpenApi/gen/XmlCommentGenerator.Emitter.cs Outdated
Comment thread src/OpenApi/gen/XmlCommentGenerator.Emitter.cs Outdated
Comment thread src/OpenApi/gen/XmlCommentGenerator.Emitter.cs Outdated
Comment thread src/OpenApi/gen/XmlCommentGenerator.Emitter.cs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

@khellang

khellang commented Jul 7, 2026

Copy link
Copy Markdown
Member Author

Rebased and fixed latest comments. Sorry for the long delay 😞

@khellang khellang force-pushed the fix/openapi-name-provider branch from 366015a to 1edd423 Compare July 8, 2026 11:34
@Youssef1313 Youssef1313 self-assigned this Jul 8, 2026
@Youssef1313 Youssef1313 removed this from the 11.0-preview6 milestone Jul 8, 2026
@Youssef1313

Copy link
Copy Markdown
Member

@khellang This fixes #65805 as well right?

@khellang

khellang commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

@khellang This fixes #65805 as well right?

Indeed. The PR predates the issue itself, but added a mention in the description now 👍

khellang and others added 17 commits July 8, 2026 23:20
…eter is bound to it

This avoids applying documentation for parameters that aren't part of
the OpenAPI surface, like CancellationToken or [FromServices] parameters,
to the request body description.
- Compute model names once per parameter instead of recomputing in
  GetOperationParameter and IsRequestBodyParameter
- Treat form/form-file binding sources as request-body parameters so
  form parameter documentation is applied to the request body
- Guard the property-loop request-body fallback with the binding source
  so only body-bound properties apply their docs to the request body
…erit attributes

- Simplify GetModelNames call to use parameterComment.Name directly
- Add GetPropertyInfo helper to avoid AmbiguousMatchException on shadowed properties while preserving inherited-property resolution
- Read model-name attributes with inherit: true
@khellang khellang force-pushed the fix/openapi-name-provider branch from d84c6aa to eb4cb7e Compare July 8, 2026 21:20
khellang added 2 commits July 8, 2026 23:47
Covers the case where a bound model inherits a documented property from a
base type. The property is enumerated under the derived ContainerType while
its DeclaringType is the base, so resolving it requires walking the hierarchy
(a lookup restricted to the container type alone would drop the description).
Covers a bound model that redeclares a base property with `new` and a
different type. A plain Type.GetProperty(name) throws AmbiguousMatchException
for this shape; the property lookup must resolve the most-derived declaration
and apply its documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc 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 pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

9 participants