-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Setting the Description in a ProducesResponseTypeAttribute works correctly for Minimal API #60539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
captainsafia
merged 10 commits into
dotnet:main
from
sander1095:fix-openapi-description-bug-minimal-api
Mar 11, 2025
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aee729a
Improve existing test
sander1095 ef31d31
Add more tests for regression
sander1095 4730e8f
Add tests which now succeed
sander1095 38606e9
Add more tests
sander1095 cf68bbe
Merge branch 'dotnet:main' into fix-openapi-description-bug-minimal-api
sander1095 917bacc
Merge remote-tracking branch 'origin/main' into fix-openapi-descripti…
sander1095 6fcf274
Set the description to the final one encountered and add tests
sander1095 a083307
Merge remote-tracking branch 'origin/main' into fix-openapi-descripti…
sander1095 fad4923
Use local method as requested by PR comment
sander1095 636c009
Fix comment
sander1095 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason the existing logic in the ApiResponseTypeProvider doesn't handle this? That feels like the right place to do this instead of having to filter on the types returned by it to set the description.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @captainsafia ! Great question! We need to discuss how we want to approach this.
Short answer: The
ApiRespnseTypeProvider
does set the description, but theEndpointMetadataProvider
throws it away. I found out about this specific behavior thanks to your comment. Below is the explanation and some suggestions on ways we could perhaps make the code a bit more clear:I dove a bit deeper into this, and this is what happens:
The code above your comment does this (
AddSupportedResponseTypes()
inEndpointMetadataApiDescriptionProvider.cs
)responseProviderMetadataTypes
contains the Description set inApiResponseTypeProvider
, as you correctly said!producesResponseMetadataTypes
does not contain the Description.Next, the following happens:
SupportedResponseTypes
is eventually returned and used.As you can see, both lists are combined with
producesResponseMetadataTypes
being iterated over first, and thus that's the firstapiResponseType
that's added to thesupportedResponseTypes
, causing it to not contain a description.At some point we loop over the
responseProviderMetadataTypes
which DO contain the description, but that doesn't get added to the list ofsupportedResponseTypes
because that type already exists thanks to the metadata.My code forces the Description to be set when we loop over the first iteration of the metadata, which fixes the issue. But this is a bit confusing, now that I understand the code a bit better.
What would you like as a solution? The core problem is that metadata is iterated over first, which doesn't contain the description. It then fails to enrich the response types from
ApiDescriptionProvider
which supplies the description.IProducesResponseTypeMetadata
should also have a Description.endpointMetadata.GetOrderedMetadata<IProducesResponseTypeMetadata>();
. This has aDescription
property, but isnull
. I'm not sure if this is intended, or if I forgot to set the Description for metadata in a previous PR. (Please advise if this is desirable/possible).IProducesResponseTypeMetadata
has aDescription
, I will need to make that happen AND update line 247 inApiDescriptionProvider
to set the description.responseProviderMetadataTypes
. This makes the code more explicit.This also explains why this bug happens in Minimal API's and not in Controllers. I believe
EndpointMetadataApiDescriptionProvider
is only used for Minimal API, whereasApiDescriptionProvider
is used for both Controllers and Minimal API. Because theEndpointMetadataApiDescriptionProvider
forgets to set enrich response types it has already added to the list, the description goes missing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sander1095 Thanks for digging into this and sharing detailed notes. Based on this analysis, I expect this option:
To be the prefered one since it aligns most closely with the way metadata gets respected in other scenarios. We'll have to sanity check this implementation with multiple
ProducesResponseType
attributes on the same endpoint to make sure the additions only apply to the target type and status code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@captainsafia Done! See the latest commits