Skip to content

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

Open
@ascott18

Description

@ascott18

Is there an existing issue for this?

  • 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:

// If there are no body parameters, check for form parameters.
// Note: Form parameters and body parameters cannot exist simultaneously
// in the same endpoint.
. 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:

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

    [HttpPost]
    [Consumes("application/x-www-form-urlencoded")]
    public IActionResult PostForm([FromForm] IEnumerable<int> 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:

    "/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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions