You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Create a new ASP.NET Core Web API project from the template of ASP.NET Core 2.2
Add a simple model class and a controller that returns it.
Apply the DefaultApiConventions (only needed to also see the error responses)
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
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.
Describe the bug
The ApiExplorer always seems to return the same three
ApiResponseFormat
entries (text/plain
,application/json
andtext/json
) even if those are not really supported or returned by the describedApiResponseType
.To Reproduce
Steps to reproduce the behavior:
DefaultApiConventions
(only needed to also see the error responses)ApiResponseType.ApiResponseFormats
of your action methods (returned by an injected instance of typeIApiDescriptionGroupCollectionProvider
).Expected behavior
The returned
ApiResponseType.ApiResponseFormat
entires should be correct depending on the value ofApiResponseType.ModelMetadata
(orApiResponseType.Type
).A "correct" entry is one that:
A) Is returned in the
Content-Type
header if requested with theAccept
header (or if noAccept
request header is given).B) Doesn't produce a
406 Not Acceptable
ifMvcOptions.ReturnHttpNotAcceptable
is set totrue
and the format is requested with theAccept
header.Some examples:
application/json
andtext/json
ProblemDetails
model should returnapplication/problem+json
string
should returntext/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 (notext/plain
and different JSON formats because ofjson-patch
). I haven't analyzedPUT
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
totrue
and use the Swagger-UI to test your API because you always get a406 Not Acceptable
back by default since Swagger-UI selects the first availableAccept
header by default, which istext/plain
.Output of
dotnet --info
:Click to expand
.NET Core SDK (gemäß "global.json"): Version: 2.2.106 Commit: aa79b139a8Laufzeitumgebung:
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
The text was updated successfully, but these errors were encountered: