Skip to content

ApiExplorer returns wrong ApiResponseFormat entries #9324

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

Closed
cremor opened this issue Apr 12, 2019 · 1 comment
Closed

ApiExplorer returns wrong ApiResponseFormat entries #9324

cremor opened this issue Apr 12, 2019 · 1 comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Milestone

Comments

@cremor
Copy link
Contributor

cremor commented Apr 12, 2019

Describe the bug

The ApiExplorer always seems to return the same three ApiResponseFormat entries (text/plain, application/json and text/json) even if those are not really supported or returned by the described ApiResponseType.

To Reproduce

Steps to reproduce the behavior:

  1. Create a new ASP.NET Core Web API project from the template of ASP.NET Core 2.2
  2. Add a simple model class and a controller that returns it.
  3. Apply the DefaultApiConventions (only needed to also see the error responses)
  4. Check the collections ApiResponseType.ApiResponseFormats of your action methods (returned by an injected instance of type IApiDescriptionGroupCollectionProvider).

Expected behavior

The returned ApiResponseType.ApiResponseFormat entires should be correct depending on the value of ApiResponseType.ModelMetadata (or ApiResponseType.Type).

A "correct" entry is one that:

A) Is returned in the Content-Type header if requested with the Accept header (or if no Accept request header is given).
B) Doesn't produce a 406 Not Acceptable if MvcOptions.ReturnHttpNotAcceptable is set to true and the format is requested with the Accept header.

Some examples:

  • Most return types, especially all user defined models, should return application/json and text/json
  • A ProblemDetails model should return application/problem+json
  • string should return text/plain (as documented)

Of course non-default response formatters or explicit Produces attributes might change that, but you get the idea.

Note: A PUT seems to already work a bit different because it returns different formats (no text/plain and different JSON formats because of json-patch). I haven't analyzed PUT further.

Additional context

I noticed this problem because Swashbuckle.AspNetCore relies on the ApiExplorer and creates an API documentation with wrong response formats because of it. It is also very annoying if you have set MvcOptions.ReturnHttpNotAcceptable to true and use the Swagger-UI to test your API because you always get a 406 Not Acceptable back by default since Swagger-UI selects the first available Accept header by default, which is text/plain.

Output of dotnet --info:

Click to expand .NET Core SDK (gemäß "global.json"): Version: 2.2.106 Commit: aa79b139a8

Laufzeitumgebung:
OS Name: Windows
OS Version: 10.0.16299
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.2.106\

Host (useful for support):
Version: 2.2.4
Commit: f95848e524

.NET Core SDKs installed:
2.1.505 [C:\Program Files\dotnet\sdk]
2.2.106 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download

@pranavkm pranavkm added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-blazor Includes: Blazor, Razor Components and removed area-blazor Includes: Blazor, Razor Components labels Apr 12, 2019
@mkArtakMSFT mkArtakMSFT added this to the 3.1.0 milestone May 20, 2019
@mkArtakMSFT mkArtakMSFT modified the milestones: 3.1.0, 5.0.0-preview1 Aug 27, 2019
@pranavkm pranavkm removed their assignment Jan 27, 2020
@mkArtakMSFT
Copy link
Contributor

Hi. Thanks for contacting us.
We're closing this issue as there was not much community interest in this ask for quite a while now.

@ghost ghost locked as resolved and limited conversation to collaborators Jun 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

3 participants