Skip to content

WithOpenApi adds Empty requestBody when parameter is of type string[] or StringValues #47644

Closed
@cleftheris

Description

@cleftheris

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When mapping any operation that has no actual body and one of the method argumets is of type string[]? or StringValues according to the documentation the api should infer that to be from query string unless explicitly configured otherwise.

Although this actually works fine for calling the endpoint, when generating documentation an empty body appears.
This happens by simply using the WithOpenApi extension method. When I debug I find that the operation.RequestBody parameter is populated (should be null if you check with other operations).

The operation

// Bind to a string array.
// GET /tags2?names=john&names=jack&names=jane
app.MapGet("/tags2", (string[] names) =>
            $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}").WithOpenApi();

The generated swagger json

{
  "openapi": "3.0.1",
  "info": {
    "title": "WebApplication1",
    "version": "1.0"
  },
  "paths": {
    "/tags2": {
      "get": {
        "tags": [
          "WebApplication1"
        ],
        "parameters": [
          {
            "name": "names",
            "in": "query",
            "required": true,
            "style": "form",
            "schema": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": { }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": { }
}

Link to the documentation

Expected Behavior

There should not be a requestBody at all there.

{
  "openapi": "3.0.1",
  "info": {
    "title": "WebApplication1",
    "version": "1.0"
  },
  "paths": {
    "/tags2": {
      "get": {
        "tags": [
          "WebApplication1"
        ],
        "parameters": [
          {
            "name": "names",
            "in": "query",
            "required": true,
            "style": "form",
            "schema": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": { }
}

Steps To Reproduce

I have prepared a project here https://github.com/cleftheris/WithOpenApiBug

It is just the default aspnet 7 template with the below code added to the Project.cs

// Bind to a string array.
// GET /tags2?names=john&names=jack&names=jane
app.MapGet("/tags2", (string[] names) =>
            $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}").WithOpenApi();

Exceptions (if any)

No response

.NET Version

7.0.4

Anything else?

.NET SDK:
Version: 7.0.202
Commit: 6c74320bc3

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

Host:
Version: 7.0.4
Architecture: x64
Commit: 0a396acafe
Screenshot 2023-04-11 161249

Metadata

Metadata

Assignees

Labels

old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions