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

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

Open
1 task done
keahpeters opened this issue Apr 28, 2024 · 2 comments
Open
1 task done
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi
Milestone

Comments

@keahpeters
Copy link

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.

@amcasey
Copy link
Member

amcasey commented Apr 29, 2024

FYI @captainsafia

@captainsafia
Copy link
Member

@keahpeters Thanks for reporting this issue!

I'll take a look at the feasibility of making a change to ApiResponseProvider, considering that OpenApi now supports oneOf to represent unions of types. We'll have to approach this area with caution given it hasn't changed much and this modification would be considered a breaking behavioral change.

Particular for attribute-based applications, we tend to follow the model that last-in wins which is why you see ApiResponseProvider modeling that behavior. It might be possible to change this by supporting things like the Results union type in particular (see here).

@captainsafia captainsafia added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks labels May 3, 2024
@captainsafia captainsafia added this to the 9.0.0 milestone May 3, 2024
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 area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi
Projects
None yet
Development

No branches or pull requests

3 participants