Skip to content

Support multiple response types with the same status code in ApiExplorer  #55412

Closed
@keahpeters

Description

@keahpeters

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

When using the ProducesResponseType attribute to specify multiple response types with the same status code only the last specified status code will be exposed. This limitation affects scenarios where an endpoint might return different types for the same HTTP status code based on business logic. For example in the following the Foo type would not be included in the response metadata.

[HttpGet]
[ProducesResponseType(typeof(Foo), 200)] // This gets ignored
[ProducesResponseType(typeof(Bar), 200)]
public IActionResult Get(bool foo)
{
    if (foo)
    {
        return Ok(new Foo());
    }

    return Ok(new Bar());
}

This is due to the status code being used as the key in the results dictionary when reading the response metadata https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.ApiExplorer/src/ApiResponseTypeProvider.cs#L210.

Describe the solution you'd like

All specified response types to be included in SupportedResponseTypes when there are multiple response types with the same status code.

Additional context

Related to domaindrivendev/Swashbuckle.AspNetCore#2743.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions