Skip to content
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

Description parameter of ProducesResponseTypeAttribute does not work in minimal API app #60518

Open
1 task done
mikekistler opened this issue Feb 20, 2025 · 4 comments · May be fixed by #60539
Open
1 task done

Description parameter of ProducesResponseTypeAttribute does not work in minimal API app #60518

mikekistler opened this issue Feb 20, 2025 · 4 comments · May be fixed by #60539
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi help wanted Up for grabs. We would accept a PR to help resolve this issue
Milestone

Comments

@mikekistler
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

A Description parameter was added to ProducesResponseTypeAttribute in .NET 10 to set the description field of the response in the generated OpenAPI document. This works fine in a controller-based app but does not seem to work in a Minimal API app.

Expected Behavior

The ProducesResponseTypeAttribute, when placed on the delegate parameter of a minimal API app endpoint, should add a description to the response in the generated API.

app.MapGet("/weatherforecast",
[ProducesResponseType<IEnumerable<WeatherForecast>>(StatusCodes.Status200OK, Description = "The service is healthy.")]
() =>
{

Steps To Reproduce

I've reproduced the problem in the project in this repo:

https://github.com/mikekistler/ResponseDescription

The problem exists for both the OpenAPI generated at build time and the OpenAPI served from the /openapi/v1.json endpoint.

Exceptions (if any)

No response

.NET Version

10.0.100-preview.1.25104.26

Anything else?

No response

@gfoidl gfoidl added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks labels Feb 20, 2025
@captainsafia
Copy link
Member

@sander1095 It looks like there's a bug with the attribute-based approach for setting Descriptions on responses in minimal APIs. It seems to only work for methods that do not return a type. The test coverage introduced in #58193 didn't cover this case so that's why we missed this.

Would you be interested in looking at a fix for this?

@captainsafia captainsafia added this to the Backlog milestone Feb 20, 2025
@captainsafia captainsafia added the help wanted Up for grabs. We would accept a PR to help resolve this issue label Feb 20, 2025
@sander1095
Copy link
Contributor

Hey @captainsafia . I apologize for this!

I am definitely interested to look for a fix for this ASAP! I hope to have some time tomorrow; otherwise I'll probably look into this during the weekend or the beginning of next week. I'll also try to add some tests to prevent this bug from reoccurring in the future.

@sander1095
Copy link
Contributor

I'll dive deeper (and properly) into this, but it does look like I added a test case for this exact scenario:

https://github.com/dotnet/aspnetcore/blob/main/src%2FOpenApi%2Ftest%2FMicrosoft.AspNetCore.OpenApi.Tests%2FServices%2FOpenApiDocumentService%2FOpenApiDocumentServiceTests.Responses.cs#L309

@captainsafia Did you accidentally skip over that test? Or do you mean something else when you say that there wasn't a test for this scenario? If there's a specific place where one should be stored, I'd love to know to reduce some research time 😊

@captainsafia
Copy link
Member

@sander1095 I meant we don't have coverage for something that looks like this:

 builder.MapGet("/api", [ProducesResponseType(typeof(TimeSpan), StatusCodes.Status201Created, Description = "foo")]
    () => TimeSpan.FromSeconds(200));

The tests currently only do this:

 builder.MapGet("/api", [ProducesResponseType(typeof(TimeSpan), StatusCodes.Status201Created, Description = "foo")]
    () => {});

The difference in behavior is different when there is an actual return type from the handler method vs. when the return type is just defined in the attribute and I assume that is what is causing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi help wanted Up for grabs. We would accept a PR to help resolve this issue
Projects
None yet
4 participants