Skip to content

WithOpenAPI Generates Empty requestBody.content #54189

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
LouisTrinczek opened this issue Feb 23, 2024 · 5 comments · Fixed by #54361
Closed
1 task done

WithOpenAPI Generates Empty requestBody.content #54189

LouisTrinczek opened this issue Feb 23, 2024 · 5 comments · Fixed by #54361
Labels
old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Comments

@LouisTrinczek
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am facing the same issues with Microsoft.AspNetCore.OpenAPI 8.0.2 as mentioned in this issue #47644. Apparently this has been fixed, but we are encountering the same issues on the most recent version as well.
The only difference is, that we are injecting a Service using [FromServices].

  "/route": {
      "get": {
        "tags": [
          "tag"
        ],
        "summary": "get xy",
        "operationId": "getXy",
        "requestBody": {
          "content": { }
        },
        "responses": { 
...

This is currently breaking our frontend API Client Generation with NSwag, because usually a GET Request doesn't allow a body, but it is being generated, due to the wrong generation of the swagger.json.

Expected Behavior

Generated JSON without requestBody

  "/route": {
      "get": {
        "tags": [
          "tag"
        ],
        "summary": "get xy",
        "operationId": "getXy",
        "responses": { 
...

Steps To Reproduce

    private static IEndpointRouteBuilder MapXy(this IEndpointRouteBuilder app)
    {
        var endpoints = app.MapGroup("/xy");
        
        
        endpoints.MapGet(
            "/",
            async ([FromServices] IXyService xyService) =>
            {
                var xy = await xyService.ListAllAsync();
                return TypedResults.Ok(xy);
            }
        ).WithOpenApi(openapi => new(openapi)
        {
            OperationId = "getXy",
            Summary = "Get xy",
            Tags = new List<OpenApiTag>() {new OpenApiTag() {Name = "tag"}}
        }).Produces<IReadOnlyList<XyDto>>(statusCode: StatusCodes.Status200OK);

        return app;
    }

Exceptions (if any)

No response

.NET Version

8.0.200

Anything else?

.NET SDK:
Version: 8.0.200
Commit: 438cab6a9d
Workload version: 8.0.200-manifests.e575128c

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.200\

.NET workloads installed:
There are no installed workloads to display.

Host:
Version: 8.0.2
Architecture: x64
Commit: 1381d5ebd2

.NET SDKs installed:
7.0.406 [C:\Program Files\dotnet\sdk]
8.0.200 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.27 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.27 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found

@ghost ghost added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Feb 23, 2024
@mkArtakMSFT mkArtakMSFT added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Feb 23, 2024
@LouisTrinczek
Copy link
Author

Any news on this?

@LouisTrinczek
Copy link
Author

Right now this would be a Workaround. Just applying a filter and nulling the RequestBody whenever the HttpMethod is "GET".

public class HttpGetBodyFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (context.ApiDescription.HttpMethod is not "GET")
        {
            return;
        }

        if (operation.RequestBody is not null)
        {
            operation.RequestBody = null;
        }
    }
}

@amcasey amcasey 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-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions labels Mar 4, 2024
@captainsafia
Copy link
Member

@LouisTrinczek Thanks for reporting this issue!

I've tracked down that this seems to only repro when using the copy constructor provided for OpenApiOperation by the Microsoft.OpenApi package. It doesn't repro if you use the setters on the provided OpenAPI like so:

.WithOpenApi(openApi => {
    openApi.OperationId = "GetWeatherForecast";
    openApi.Summary = "Get weather forecast";
    openApi.Tags = new List<OpenApiTag>() {new OpenApiTag() {Name = "WeatherForecast"}};
    return openApi;
});

It looks like the underlying issue was fixed in microsoft/OpenAPI.NET#1195. We'll need to bump up the version of Microsoft.OpenApi we use in our package to resolve this.

@LouisTrinczek
Copy link
Author

LouisTrinczek commented Mar 5, 2024

Thank you!
@captainsafia When is the fix expected to be available for us, or in which version?

@captainsafia
Copy link
Member

@LouisTrinczek I've only merged the fix into .NET 9 for now. Locally, you should be able to resolve this by bumping up the version of Microsoft.OpenApi package that you use. I believe the transitive dependencies will get sorted out.

dotnet add package Microsoft.OpenApi --version 1.6.13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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