-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Media type application/problem+json still lost in combination with ProducesAttribute #39802
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
Comments
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: Repro details✅ Test 1 - Problem() without [Produces]Returns curl -i http://localhost:5106/test/1 HTTP/1.1 500 Internal Server Error
Content-Type: application/problem+json; charset=utf-8
Date: Thu, 27 Jan 2022 07:53:16 GMT
Server: Kestrel
Transfer-Encoding: chunked
{"type":"https://tools.ietf.org/html/rfc7231#section-6.6.1","title":"An error occurred while processing your request.","status":500,"detail":"Test1 ✅ - Problem(string) without ProducesAttribute => returns application/problem+json. Assembly: C:\\Program Files\\dotnet\\shared\\Microsoft.AspNetCore.App\\6.0.1\\Microsoft.AspNetCore.Mvc.Core.dll","traceId":"00-f837e61b4f3fe38e1326d4b736ce4b1b-5f42df452447a45b-00"} ❌ Test 2 - Problem() with [Produces("application/json")]Returns curl -i http://localhost:5106/test/2 HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8
Date: Thu, 27 Jan 2022 07:54:47 GMT
Server: Kestrel
Transfer-Encoding: chunked
{"type":"https://tools.ietf.org/html/rfc7231#section-6.6.1","title":"An error occurred while processing your request.","status":500,"detail":"Test2 ❌ - Problem(string) with [Produces(\"application/json\")] => incorrectly returns application/json instead of application/problem+json. Assembly: C:\\Program Files\\dotnet\\shared\\Microsoft.AspNetCore.App\\6.0.1\\Microsoft.AspNetCore.Mvc.Core.dll","traceId":"00-7898b5db7c72df8b1589c0776c857321-1145d480210ea6e5-00"} ❌ Test 3 - Problem() with [Produces("application/json", "application/problem+json")]Returns curl -i http://localhost:5106/test/3 HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8
Date: Thu, 27 Jan 2022 08:50:00 GMT
Server: Kestrel
Transfer-Encoding: chunked
{"type":"https://tools.ietf.org/html/rfc7231#section-6.6.1","title":"An error occurred while processing your request.","status":500,"detail":"Test3 ❌ - Problem(string) with [Produces(\"application/json\", \"application/problem+json\")] => incorrectly returns application/json instead of application/problem+json. Assembly: C:\\Program Files\\dotnet\\shared\\Microsoft.AspNetCore.App\\6.0.1\\Microsoft.AspNetCore.Mvc.Core.dll","traceId":"00-4e825df690029941d19792a19b6e1346-1e417b4f47ca76cf-00"} ❌ Test 4 - Problem() with [Produces("application/problem+json", "application/json")]Returns curl -i http://localhost:5106/test/4 HTTP/1.1 500 Internal Server Error
Content-Type: application/problem+json; charset=utf-8
Date: Thu, 27 Jan 2022 07:55:05 GMT
Server: Kestrel
Transfer-Encoding: chunked
{"type":"https://tools.ietf.org/html/rfc7231#section-6.6.1","title":"An error occurred while processing your request.","status":500,"detail":"Test4 ❌ - Problem(string) with [Produces(\"application/problem+json\", \"application/json\")] => returns content-type application/problem+json, but then Ok(string) also returns application/problem+json. Assembly: C:\\Program Files\\dotnet\\shared\\Microsoft.AspNetCore.App\\6.0.1\\Microsoft.AspNetCore.Mvc.Core.dll","traceId":"00-cb867abbe8d88fbced809ff7764199fe-bb8a5378a54e6789-00"} dotnet --info``` dotnet --info .NET SDK (reflecting any global.json): Version: 6.0.101 Commit: ef49f6213aRuntime Environment: Host (useful for support): .NET SDKs installed: .NET runtimes installed:
|
So, the
aspnetcore/src/Mvc/Mvc.Core/src/ProducesAttribute.cs Lines 101 to 108 in b21596a
So, this is going to set I believe that the tests introduced in that PR didn't catch the issue because they don't account for the fact that the I think the fundamental right choice here is to modify the |
Thanks for contacting us. We're moving this issue to the |
@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... |
Fingers crossed this gets fixed. Right now, I have to create an extra operation filter in every API I create. |
Just stumbled on this: For our JSON APIs, this means We have generally transitioned from |
One reason we use --- edit --- |
Same here. We need to be able to use both and get the correct media types. |
Thanks for contacting us. We're moving this issue to the |
I know this sounds silly but this was actually my most anticipated "feature" of .Net 7. |
Same here. I came here hoping to see it fixed. |
@brunolins16 What do you think about the proposed solution here? |
Is this going to get fixed? |
still waiting the fix |
I found a workaround. If I remove the [ProducesResponseType(typeof(MyResponseType), statusCode: 200, "application/json")]
[ProducesResponseType(typeof(ProblemDetails), statusCode: 400, "application/problem+json")]
[ProducesResponseType(typeof(ProblemDetails), statusCode: 500, "application/problem+json")] 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 |
Assigning myself to this to see if I can land on a solution for this as part of the recent wave of OpenAPI-work. I'm particularly eyeing the change in behavior between I have to revisit the analysis I did in #39802 (comment). Hopefully, there is a straightforward way to apply this fix without having to considerably break into the content-neg layer in MVC. |
Is there an existing issue for this?
Describe the bug
This issue is the same as reported and resolved here: #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 toapplication/json
will set the response headermedia-type
toapplication/json
when it's returning aProblemDetails
orValidationProblemDetails
. It's supposed to returnapplication/problem+json
.Expected Behavior
When the controller or framework returns a
ProblemDetails
orValidationProblemDetails
, the response headermedia type
should beapplication/problem+json
as per https://datatracker.ietf.org/doc/html/rfc7807#section-3Steps 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 responsemedia type
will beapplication/json
instead ofapplication/problem+json
.Exceptions (if any)
No response
.NET Version
6.0.101
Anything else?
dotnet --info
The text was updated successfully, but these errors were encountered: