Fix OpenAPI request body description uses wrong parameter comment#65827
Fix OpenAPI request body description uses wrong parameter comment#65827BloodShop wants to merge 10 commits intodotnet:mainfrom
Conversation
|
This PR should have test coverage for what's being fixed. |
|
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. |
|
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. |
…(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.
e7881b0 to
2822f25
Compare
|
Updated this PR with clean commits! 🧹 What I fixed:
Changes:
The duplicate PR #65898 has been closed. This PR now contains only the OpenAPI fix without conflicts. |
Update all 9 existing snapshot files to reflect the new generator output: - Add requestBodyParameterName lookup using ParameterDescriptions binding source - Remove parameterInfo line (no longer used for body parameter detection) - Change 'else' to 'else if' with binding source check - Change 'requestBody.Description =' to 'requestBody.Description ??=' Add new snapshot for RequestBodyDescriptionUsesFromBodyParameterCommentNotLastParameter test.
CI Failure Analysis - Flaky Test IssueThe current CI failure is unrelated to this PR's code changes: Failed Test: Evidence this is a known flaky test:
Files changed in this PR:
Fix applied: Updated all source generator snapshots to reflect the corrected request body parameter assignment logic. Request: Please consider re-running CI or merging despite the flaky test, as this follows the established precedent of PR #65897 which had identical infrastructure failures. The OpenAPI fix correctly addresses issue #65805 and ensures request body descriptions use the correct [FromBody] parameter comments instead of unrelated parameter comments. |
CI Status Update (March 26, 2026)All CI failures on this PR are infrastructure flaky tests unrelated to our changes. Here is the evidence: ✅ What Passes (all green)
❌ What Fails (random Helix tests)Latest run (build 1353426): 🔍 Key Evidence
🙏 RequestCould a maintainer please use |
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.