Skip to content

Respect IModelNameProvider when matching OpenAPI parameters - #64535

Merged
Youssef1313 merged 21 commits into
dotnet:mainfrom
khellang:fix/openapi-name-provider
Jul 23, 2026
Merged

Youssef1313 merged 21 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

@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.

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.

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 and others added 6 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.
@Youssef1313

Copy link
Copy Markdown
Member

Hi @khellang

I'd like to get this in .NET 11 Preview 7. Are you planning to complete the work here please? Let me know if there is something we can help with, or if you want us to take over the PR to completion.

@khellang

Copy link
Copy Markdown
Member Author

Great! I'm not aware of any remaining work?

@Youssef1313

Copy link
Copy Markdown
Member

@khellang The build is failing with:

/Users/runner/work/1/s/artifacts/obj/Sample/Release/net11.0/generated/Microsoft.AspNetCore.OpenApi.SourceGenerators/Microsoft.AspNetCore.OpenApi.SourceGenerators.XmlCommentGenerator/OpenApiXmlCommentSupport.generated.cs(593,36): error IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperty(String, BindingFlags)'. The parameter 'containerType' of method 'Microsoft.AspNetCore.OpenApi.Generated.XmlCommentOperationTransformer.GetPropertyInfo(Type, String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [/Users/runner/work/1/s/src/OpenApi/sample/Sample.csproj]
/Users/runner/work/1/s/artifacts/obj/Sample/Release/net11.0/generated/Microsoft.AspNetCore.OpenApi.SourceGenerators/Microsoft.AspNetCore.OpenApi.SourceGenerators.XmlCommentGenerator/OpenApiXmlCommentSupport.generated.cs(593,36): error IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperty(String, BindingFlags)'. The return value of method 'System.Type.BaseType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [/Users/runner/work/1/s/src/OpenApi/sample/Sample.csproj]

@khellang

khellang commented Jul 16, 2026 •

Copy link
Copy Markdown
Member Author

OK, I'm on vacation and probably wont be able to get it fixed right away. Let me see if I can get Claude to fix it...

…nt transformer

The generated XmlCommentOperationTransformer.GetPropertyInfo helper reflects
over the container type (from ModelMetadata.ContainerType) via Type.GetProperty
and a BaseType walk. That container type is not statically annotated for
trimming, so the emitted code produced IL2070/IL2075 analyzer errors when the
OpenApi Sample project is built with trim analysis enabled.

Annotating the parameter would only move the warning to the call site (the
source Type is runtime model metadata). Instead, suppress IL2070/IL2075 on the
helper: if a property has been trimmed, GetProperty returns null and the XML
documentation is simply not applied, so this degrades safely.

Applied identically to the source generator emitter template and all affected
Verify snapshots so the source-generator snapshot tests stay in sync.
@khellang

Copy link
Copy Markdown
Member Author

Better now?

@Youssef1313
Youssef1313 merged commit a1c5f7c into dotnet:main Jul 23, 2026
24 checks passed
@dotnet dotnet deleted a comment from github-actions Bot Jul 23, 2026
@dotnet dotnet deleted a comment from github-actions Bot Jul 23, 2026
@Youssef1313

Copy link
Copy Markdown
Member

/backport to release/11.0-preview7

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0-preview7 (link to workflow run)

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

10 participants