Repository navigation
Respect IModelNameProvider when matching OpenAPI parameters - #64535
Conversation
There was a problem hiding this comment.
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
GetOperationParameterhelper method that considers both the parameter's original name and any custom names fromIModelNameProviderattributes - Adds early null check for
parameterInfobefore 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 |
68d5b6f to
16a616e
Compare
16a616e to
d6a6aaa
Compare
…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
d84c6aa to
eb4cb7e
Compare
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.
|
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. |
|
Great! I'm not aware of any remaining work? |
|
@khellang The build is failing with: |
|
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.
|
Better now? |
|
/backport to release/11.0-preview7 |
|
Started backporting to |
Description
This makes sure any attribute implementing
IModelNameProvideris respected when matching OpenAPI operation parameters with .NET action parameters.It also replaces the
CancellationTokenworkaround from the initial review with a proper check againstBindingSource.Body, so documentation for parameters that aren't part of the OpenAPI surface (e.g.CancellationTokenor[FromServices]parameters) is no longer applied to the request body description.Fixes #64534
Fixes #64521
Fixes #67076
Fixes #65805