dotnet / dotnet/aspnetcore

.NET 9 OpenAPI doesn't support [Consumes] multiple content types correctly.

Open
#58,329 3 comments 2 reactions 0 assignees View on GitHub
area-mvc feature-openapi
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

Routes that accept both form and json bodies through multiple controller actions with [ConsumesAttribute], a documented, supported use case of aspnetcore, are not emitted correctly by Microsoft.AspNetCore.OpenApi.

The comment on these lines is incorrect: https://github.com/dotnet/aspnetcore/blob/44a9f8a83a1d20a3e367d5ec0d82111e84c7e5ec/src/OpenApi/src/Services/OpenApiDocumentService.cs#L478-L480. It contradicts the documentation for [ConsumesAttribute] - https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-9.0#define-supported-request-content-types-with-the-consumes-attribute-1. A single route can indeed support both formdata and json bodies.

### Expected Behavior

Both JSON and form content types are listed for the endpoint in `requestBody.content` in the OpenAPI document.

### Steps To Reproduce

Write the exact example from the [Consumes] documentation at https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-9.0#define-supported-request-content-types-with-the-consumes-attribute-1:

``` c#
[ApiController]
[Route("api/[controller]")]
public class ConsumesController : ControllerBase
{
[HttpPost]
[Consumes("application/json")]
public IActionResult PostJson(IEnumerable values) =>
Ok(new { Consumes = "application/json", Values = values });

[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public IActionResult PostForm([FromForm] IEnumerable values) =>
Ok(new { Consumes = "application/x-www-form-urlencoded", Values = values });
}
```

Observe that the JSON body is excluded from the OpenAPI document produced by `Microsoft.AspNetCore.OpenApi`. Only the form data body is present:

``` json
"/api/Consumes": {
"post": {
"tags": [
"Consumes"
],
"requestBody": {
"content": {
"application/x-www-form-urlencoded": {
"schema": {
"type": "object",
"properties": {
"values": {
"type": "array",
"items": {
"type": "integer",
"format": "int32"
}
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK"
}
}
}
},
```

### Exceptions (if any)

_No response_

### .NET Version

9.0.100-rc.2.24474.11

### Anything else?

_No response_

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.