Search code examples
c#swagger-uiswashbuckle.aspnetcoreminimal-apis

In Swashbuckle.AspNetCore, Swagger JSON and UI generated without content type when Results.Ok is used


When you define API end points like following both are same in functionality,

        app.MapPost("/todoitems", async (FileModel file) =>
        {
            return file;
        });
        app.MapPost("/todoitemsnew", async (FileModel file) =>
        {
            return Results.Ok(file);
        });

Both APIs should generate same UI and JSON output, but the point with Results.Ok is generated as no return content end point.

When a return object is used then the generated UI is like this (when return file; used) image

When a Results.Ok is used the generated UI is like this (when return Results.Ok(file); used) image

JSON generated is also missing content type.

When a return object is used then the JSON is like this (when return file; used)

"responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/FileModel"
                }
              }
            }
          } 

When a Results.Ok is used the JSON is like this (when return Results.Ok(file); used)

"responses": {
          "200": {
            "description": "Success"
          }
        }

This creates a problem when you use Nswag code generators. Not found any answer in the documentation

Is it by design ? or Bug ? Not found information on the same in the documentation.


Solution

  • You have to add explicitly the produced response in this case.

        app.MapPost("/todoitemsnew", async (FileModel file) =>
        {
            return Results.Ok(file);
        }).Produces(200,typeof(FileModel));
    

    should give you expected result.