aspnetcore: Media type application/problem+json still lost in combination with ProducesAttribute
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
This issue is the same as reported and resolved here: https://github.com/dotnet/aspnetcore/issues/19510
Having tested with the project that is the previous issue’s minimal repro (in .NET 6 instead of .NET core 3.1), I’ve found the same issue still persists.
That is, a controller with ProducesAttribute set to application/json will set the response header media-type to application/json when it’s returning a ProblemDetails or ValidationProblemDetails. It’s supposed to return application/problem+json.
Expected Behavior
When the controller or framework returns a ProblemDetails or ValidationProblemDetails, the response header media type should be application/problem+json as per https://datatracker.ietf.org/doc/html/rfc7807#section-3
Steps To Reproduce
Minimalistic repro project: https://github.com/dsun1/ProblemMediaTypeIssue
Attribute a controller class or method with ProducesAttribute("application/json"). If that controller returns a problem, or if the framework returns a validation problem, the response media type will be application/json instead of application/problem+json.
Exceptions (if any)
No response
.NET Version
6.0.101
Anything else?
dotnet --info
.NET SDK (reflecting any global.json):
Version: 6.0.101
Commit: ef49f6213a
Runtime Environment:
OS Name: Mac OS X
OS Version: 11.0
OS Platform: Darwin
RID: osx.10.16-x64
Base Path: /usr/local/share/dotnet/sdk/6.0.101/
Host (useful for support):
Version: 6.0.1
Commit: 3a25a7f1cc
.NET SDKs installed:
2.1.818 [/usr/local/share/dotnet/sdk]
3.1.416 [/usr/local/share/dotnet/sdk]
5.0.404 [/usr/local/share/dotnet/sdk]
6.0.101 [/usr/local/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.30 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.30 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.22 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.13 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.30 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.22 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.13 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 10
- Comments: 16 (5 by maintainers)
Same here. We need to be able to use both and get the correct media types.
So, the
ProducesAttributehas anOnResultExecutinghook that runs before the logic in theObjectResultExecutorthat was modified in the referenced PR runs. ThisOnResultExecutingwill clear outresult.ContentTypesand replace it with the content types that are defined in theProducesAttribute.https://github.com/dotnet/aspnetcore/blob/b21596afeb7412844ee5e19f6e5bef832c533c0f/src/Mvc/Mvc.Core/src/ProducesAttribute.cs#L91
https://github.com/dotnet/aspnetcore/blob/b21596afeb7412844ee5e19f6e5bef832c533c0f/src/Mvc/Mvc.Core/src/ProducesAttribute.cs#L101-L108
So, this is going to set
result.ContentTypesto["application/problem+json", "application/json"]. Now theInferContentTypemethod modified in the referenced PR effectively no-ops becauseresult.ContentTypesis not empty and theresult.Valueis notProblemDetails.https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Core/src/Infrastructure/ObjectResultExecutor.cs#L130-L142
I believe that the tests introduced in that PR didn’t catch the issue because they don’t account for the fact that the
ProducesAttributemodifies the content types in this way.I think the fundamental right choice here is to modify the
ProducesAttributesuch that theOnResultExecutinginvocation no-ops under certain scenarios since that’s the root cause here.I was just about to file this exact same issue, dumping my report here instead.
This was reported in #19510 and fixed in #30367, included in .NET 6.0-preview3.
I can still reproduce this issue, tested on:
Minimal repro here: https://github.com/angularsen/repro-aspnetcore-19510
Repro details
✅ Test 1 - Problem() without [Produces]
Returns
application/problem+jsonas expected.❌ Test 2 - Problem() with [Produces(“application/json”)]
Returns
application/jsoninstead of expectedapplication/problem+json.❌ Test 3 - Problem() with [Produces(“application/json”, “application/problem+json”)]
Returns
application/jsoninstead of expectedapplication/problem+json.❌ Test 4 - Problem() with [Produces(“application/problem+json”, “application/json”)]
Returns
application/problem+json, but thenOk(object)also returnsapplication/problem+jsoninstead of expectedapplication/json.dotnet --info
``` dotnet --info .NET SDK (reflecting any global.json): Version: 6.0.101 Commit: ef49f6213aRuntime Environment: OS Name: Windows OS Version: 10.0.22000 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\6.0.101\
Host (useful for support): Version: 6.0.1 Commit: 3a25a7f1cc
.NET SDKs installed: 3.1.416 [C:\Program Files\dotnet\sdk] 5.0.301 [C:\Program Files\dotnet\sdk] 5.0.302 [C:\Program Files\dotnet\sdk] 6.0.100-rc.2.21505.57 [C:\Program Files\dotnet\sdk] 6.0.100 [C:\Program Files\dotnet\sdk] 6.0.101 [C:\Program Files\dotnet\sdk]
.NET runtimes installed: Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.0-rc.2.21480.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0-rc.2.21480.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0-rc.2.21501.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
I found a workaround. If I remove the
[Produces(...)]attribute and replace it with:then the response has the correct content-type header (and swagger documentation is correct).
Or if you want to make it automatic on every endpoint:
and then I only need to add the
[ProducesResponseType(...)]attribute for the success case (200/201/204) on each controller actionI know this sounds silly but this was actually my most anticipated “feature” of .Net 7.
still waiting the fix
One reason we use
[Produces("application/json"]is because we want our swagger documentation to showapplication/jsonfor our normal content. We would be glad if this would be fixed properly (and I would even consider helping with the PR for this). For now I would probably go for a custom ProducesAttribute to “fix” the problem myself.— edit — The workaround I have chosen for now is to use
[SwaggerResponse(StatusCodes.Status200OK, null, typeof(...), MediaTypes.Application.Json)]without[Produces(MediaTypes.Application.Json)]. This will return the correct Content types in our swagger documentation and the correct content type in all responses (that I have tested)Just stumbled on this: JSON output is the default negotiated content format.
For our JSON APIs, this means
[Produces("application/json")]is largely redundant and we can remove it from our API controllers to get the correct response content type headerapplication/problem+jsonwhen returningProblem(),ValidationProblem()and other object results producing a ProblemDetails response.We have generally transitioned from
ActionResultto exception flow to control error responses with https://github.com/khellang/Middleware, but many old endpoints still return ActionResult.@brunolins16 Not sure if you want to consider this under the bucket of ProblemDetails-related items.
Not sure about relative priority compared to the cost of fix here, so might not be worth doing…