Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
cleftheris opened this issue Apr 11, 2023 · 7 comments · Fixed by #47659
Closed
1 task done
Assignees
Labels
old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Milestone

Comments

@cleftheris
Copy link

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

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Apr 11, 2023
@MackinnonBuck MackinnonBuck added old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels Apr 11, 2023
@captainsafia
Copy link
Member

@cleftheris Thanks for the bug report! I tracked it down to this line of code:

https://github.com/dotnet/dotnet/blob/8d5c52c5dd54417a2071c196663bb29ab1ee2442/src/aspnetcore/src/OpenApi/src/OpenApiGenerator.cs#L262

Fix incoming...

@cleftheris
Copy link
Author

Wow thanks @captainsafia you are very quick! Can we expect this to land on dotnet7.0 as a hot fix or I have to wait for dotnet8?

I got a couple more issues to report on OpenAPI since we migrated a large code base to minimal APIs and have had the time to check things out.

Thanks again for your work

@giorgos07
Copy link

@captainsafia thank you for your really quick fix. Do you believe this fix can be included in a minor or patch release of the Microsoft.AspNetCore.OpenApi package? A stable .NET 8 probably needs a significant amount of time to be released and it may be source incompatible or have breaking changes with our existing code base. So we will need some time to adopt it!

@captainsafia
Copy link
Member

I got a couple more issues to report on OpenAPI since we migrated a large code base to minimal APIs and have had the time to check things out.

Don't hesitate to report issues here! I look forward to polishing up this area.

Do you believe this fix can be included in a minor or patch release of the Microsoft.AspNetCore.OpenApi package?

We can push a hotfix for this. I'll re-open the issue to track backporting to the next servicing release for 7.0.

@captainsafia captainsafia reopened this Apr 13, 2023
@captainsafia captainsafia modified the milestones: 8.0-preview4, 7.0.x Apr 13, 2023
@giorgos07
Copy link

@captainsafia that sound perfect. Thanks again for your immediate action!

@captainsafia
Copy link
Member

PR to backport to .NET 7: #47807

@captainsafia
Copy link
Member

Closing since backport has been merged.

@ghost ghost locked as resolved and limited conversation to collaborators Jun 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants