Skip to content

Fix ProducesResponseType on IResult handling in OpenApiGenerator and ApiResponseTypeProvider #57801

@nicro950

Description

@nicro950

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

It looks like minimal apis adds a ProducesResponseTypeMetadata with status 200 automatically to some Map*(Get/Put/Post/...) configurations. From my testing it looks like it happens as long as the Map* is given a delegate which returns some data, including Task or Task_T. In some cases this might be what you want, but if the endpoint, for instance, only returns a 201 Created, then the endpoint metadata is reporting that it both return a 200 and 201(As long as you also adds the Produces(201) metadata), which it does not do. This became a problem for us further down the line, when we tried to generate the corresponding OpenApi document for the metadata information, which added the extra response code, and set the response type to an empty object.

As seen in the image, both endpoint which returns a Task, have the extra metadata, but the last one returning only void does not. I also tried returning a string, and then it added the 200, but that would be the correct behavior in that context. The 200 response was removed when returning an Result.NoContent() directly without having it in a Task.

image

Expected Behavior

Either that ProducesResponseTypeMetadata is never added automatically or that it is only automatically added if the endpoint is guaranteed to have a scenario which results in a 200 status code. Looks like the intention was that it gets the 200 if the endpoint returns a type, but it should not get the 200 if it returns void, IResult, Task, or Task_IResult but the last two was not included.

Steps To Reproduce

  1. Create a new asp.net core application with minimal apis
  2. Add the following endpoints:
        app.MapPost("/test", () =>
            {
                return Task.CompletedTask;
            });

        app.MapPost("/test2", () =>
            {
                return Task.FromResult(Results.NoContent);
            })
            .Produces(201);

        app.MapPost("/test3", () =>
            {
            });
  1. The first two will have the extra metadata added to them.

Exceptions (if any)

No response

.NET Version

9.0.100-rc.1.24452.12

Anything else?

No response

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

Type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions