fix: OpenAPI request body description uses correct parameter comment#65898
Closed
BloodShop wants to merge 4 commits intodotnet:mainfrom
Closed
fix: OpenAPI request body description uses correct parameter comment#65898BloodShop wants to merge 4 commits intodotnet:mainfrom
BloodShop wants to merge 4 commits intodotnet:mainfrom
Conversation
…(issue dotnet#65805) When an endpoint has multiple parameters, the request body description should use the [FromBody] parameter's XML comment, not the last unmatched parameter's comment. The bug was in the loop that assigns parameter comments. It would iterate through all parameters and assign any unmatched parameter's comment to the request body, causing the last iteration (usually an injected dependency like CancellationToken) to overwrite the correct [FromBody] parameter's comment. Fix: Only use a parameter's comment for the request body if it has a [FromBody] attribute or is a complex type (indicating it's meant for request body binding).
Verifies that when an endpoint has [FromBody] followed by other parameters ([FromServices], CancellationToken), the request body description uses the [FromBody] parameter's comment, not the last parameter's comment.
Switched from attribute-based heuristics to using the actual ParameterDescription binding source to identify which parameter comment belongs to the request body. Also prevent overwriting requestBody.Description if already set, ensuring the correct parameter's comment is preserved.
Member
|
Appears to be an exact duplicate of #65827. |
Author
|
Closing duplicate PR. Updated the original PR #65827 with clean commits instead. Sorry for the confusion! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes issue #65805 where OpenAPI request body descriptions were incorrectly using the last parameter's XML comment instead of the [FromBody] parameter's comment.
Problem
When an endpoint had multiple parameters (e.g., [FromBody] parameter followed by [FromServices] or CancellationToken), the OpenAPI generator would incorrectly assign the last parameter's XML comment to the request body description instead of using the comment from the parameter that actually represents the request body.
Root Cause
The original loop that assigns parameter comments would iterate through all parameters and assign any unmatched parameter's comment to the request body. This caused the last iteration (usually an injected dependency like CancellationToken) to overwrite the correct [FromBody] parameter's comment.
Solution
Updated the logic to:
ParameterDescription.Sourcefrom ApiExplorer to correctly identify which parameter represents the request bodyBody,FormFile, orForm)requestBody.Descriptionif already set, ensuring the correct comment is preservedFiles Modified
src/OpenApi/gen/XmlCommentGenerator.Emitter.cs- Core fix to parameter comment assignment logicsrc/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/OperationTests.MinimalApis.cs- Added regression testBenefits
Testing
Added regression test that verifies when an endpoint has [FromBody] followed by [FromServices] and CancellationToken parameters, the request body description uses the [FromBody] parameter's comment ("Sample data provided by the user") and not the last parameter's comment ("Injected cancellation token").
This is a clean version that contains only the OpenAPI fix without the infrastructure changes that were causing test failures in the previous PR.
Fixes #65805