Media type application/problem+json lost in combination with ProducesAttribute

See original GitHub issue

Describe the bug

I decorate a controller (action method) with the ProducesAttribute to limit the response media type to application/json. If I return a ProblemDetails instance from the controller (or the framework returns a ValidationProblemDetails instance), the returned media type is application/json. If I leave out the ProducesAttribute, the response has media type application/problem+json.

I am unsure if this is a bug or is by design.

What I am finally trying to achieve is to have a generated OpenAPI spec (using Swashbuckle.AspNetCore) indicating application/problem+json for my 400 responses, and application/json for my 200 responses. Swashbuckle gets its information from the API Explorer from ASP.NET Core itself, this led me to this issue (or question).

Similar issue found in this SO question (unanswered).

To Reproduce

See minimalistic repo with contrived example: https://github.com/ralphhendriks/aspnet-core-problem-details-media-type-issue

Further technical details

I am using ASP.NET Core version 3.1

Output of dotnet --info:

$ dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.102
 Commit:    573d158fea

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.102\

Host (useful for support):
  Version: 3.1.2
  Commit:  916b5cba26

.NET Core SDKs installed:
  2.1.802 [C:\Program Files\dotnet\sdk]
  3.1.101 [C:\Program Files\dotnet\sdk]
  3.1.102 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

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

IDE used seems irrelevant (using both VS 2019 Enterprise Edition and VS Code with OmniSharp plugin).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
andrii-litvinovcommented, Mar 21, 2020

I have same issue. For now I have resolved it by creating a derived ProducesAttribute with special handling for ProblemDetail:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class ProducesAttribute : Microsoft.AspNetCore.Mvc.ProducesAttribute
{
    public ProducesAttribute(Type type) : base(type)
    {
    }

    public ProducesAttribute(string contentType, params string[] additionalContentTypes) : base(contentType, additionalContentTypes)
    {
    }

    public override void OnResultExecuting(ResultExecutingContext context)
    {
        if (context.Result is ObjectResult result && result.Value is ProblemDetails) return;
        base.OnResultExecuting(context);
    }
}

Though I still haven’t found a way how to configure different content type for 4xx and 5xx responses for Swagger.

2reactions
CumpsDcommented, Mar 13, 2020

We are running into this as well now, with a client trying to generate NSwag proxy from our swashbuckle generated swagger document. We are also unable to get the 200’s to just return application json and the 400s application problem json

Read more comments on GitHub >

github_iconTop Results From Across the Web

Net Core 5 Post Response returns Incorrect Content-Type
Incorrect Content-Type: application/json. I have already configured the AddControllers(...) method in startup with the .AddNewtonsoftJson(.
Read more >
Understanding Problem JSON - Sander van Beek - Medium
Problem is a standardized way of describing any kind of error thrown by an API. A Problem can be described in JSON or...
Read more >
Creating Discoverable HTTP APIs with ASP.NET Core 5 ...
To each attribute is passed the well-known property MediaTypeNames.Application.Json , thus specifying that the only content type our API should ...
Read more >
Untitled
NET Core Media type application/problem+json lost in combination … Basic Structure - Swagger WebMar 30, 2016 · 1 I have a very strange...
Read more >
The application/problem+json Content-Type
This says that the actual format is json, but that a client can find out more about the underlying meaning or semantics of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found