Fix OpenAPI request body description uses wrong parameter comment#65827
Open
BloodShop wants to merge 4 commits intodotnet:mainfrom
Open
Fix OpenAPI request body description uses wrong parameter comment#65827BloodShop 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).
Contributor
|
Thanks for your PR, @@BloodShop. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
Member
|
This PR should have test coverage for what's being fixed. |
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.
Author
|
Test coverage added. New regression test verifies that when an endpoint has [FromBody] followed by [FromServices] and CancellationToken parameters, the request body description uses the [FromBody] parameter's XML comment, not the last parameter's. |
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.
Author
|
Fixed the test failures. The issue was using reflection-based attribute heuristics to identify the request body parameter. Switched to using ApiExplorer's ParameterDescription binding source which is deterministic and already knows which parameter is the body. |
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.
Fixes issue #65805.
When an endpoint has multiple parameters including [FromBody] and injected dependencies (like ILogger or CancellationToken), the request body description was incorrectly using the comment from the LAST parameter instead of the [FromBody] parameter.
Example:
Expected request body description: "Sample data provided by the user."
Actual request body description: "Injected cancellation token."
The bug was in XmlCommentGenerator.Emitter where it loops through all parameters and assigns any unmatched parameter comment to the request body. The last iteration would overwrite previous ones.
Fix: Only use a parameter comment for the request body if it has [FromBody] or is a complex type.