dotnet / dotnet/aspnetcore

Issue with Wildcard Media Types

Open
#18,891 3 comments 1 reaction 0 assignees View on GitHub
affected-few area-mvc bug feature-mvc-formatting investigate question severity-major
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

We have an ASP.NET core web API help pages with Swagger (swashbuckle). If there is any error , then we want to send the error with the content-type set to “application/*+json”.

Something like this appears to works:

```
namespace TodoApi.Controllers
{
#region snippet_TodoController
[Produces("application/json", "application/problem+json" , "text/plain")]
[Route("api/[controller]")]
public class TodoController : ControllerBase
{
================ Trimmed ================

[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
public IActionResult Create([FromBody] TodoItem item)
{
if (item == null)
{
ContentResult a = new ContentResult();
a.ContentType = "application/problem+json";
a.StatusCode = 400;
return a;
}

_context.TodoItems.Add(item);
_context.SaveChanges();

return CreatedAtRoute("GetTodo", new { id = item.Id }, item);
}
```

.. but we expect the [ProblemDetailsClientErrorFactory ](https://github.com/dotnet/aspnetcore/blob/master/src/Mvc/Mvc.Core/src/Infrastructure/ProblemDetailsClientErrorFactory.cs) to handle this automatically, which it appears to do, but it does not stick when the Produces Attribute is present:


![image](https://user-images.githubusercontent.com/60798232/74068817-9299dd00-49ca-11ea-8365-b21259353619.png)

If the Produces Attribute is not used, the correct content type is returned, but the metadata is incomplete (application/problem+json is missing).

**We feel the issue is here:**

The JsonOutputFormatter defines three media types (text/plan, application/json and application/*+json). The application/*+json is never returned through the API Explorer because of the wildcard handling. The API Explorer requests all supported content types from the the OutputFormatter (GetSupportedContentTypes) by passing the null value (from GetApiResponseTypes). The issue is that wildcard media types are handled differently and are never returned when null is passed.

For more details, see:

[https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonOutputFormatter.cs](https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonOutputFormatter.cs)
[https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.ApiExplorer/src/ApiResponseTypeProvider.cs (GetApiResponseTypes)](https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.ApiExplorer/src/ApiResponseTypeProvider.cs (GetApiResponseTypes))
[https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.Core/src/Formatters/OutputFormatter.cs (GetSupportedContentTypes)](https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.Core/src/Formatters/OutputFormatter.cs (GetSupportedContentTypes))

The JsonOutputFormatter adds application/*+json


![image](https://user-images.githubusercontent.com/60798232/74069095-456a3b00-49cb-11ea-8d96-10273c34637e.png)

The ApiResponseTypeProvider adds a null content type


![image](https://user-images.githubusercontent.com/60798232/74069108-53b85700-49cb-11ea-87b8-d7ce8f5d6c4c.png)

… and then requests the SupportedContentTypes with null:


![image](https://user-images.githubusercontent.com/60798232/74069137-65016380-49cb-11ea-8781-bd47087231bb.png)

This is where we believe the issue is (OutputFormatter). If the media type is not a wildcard (ex: text/plain) and you pass null, it will be added (else branch). If it’s a wildcard though (like application/*+json), it won’t be added (if branch).


![image](https://user-images.githubusercontent.com/60798232/74069185-7cd8e780-49cb-11ea-8315-b34071cbe761.png)

Is this supposed to work like this..?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.