Override response description in Minimal API

249 Views Asked by At

I'm building a simple .NET 8 (RC2) Minimal API.
I have a simple endpoint that can return one of 3 status codes: 200, 201 or 404.

app.MapGet("/check/{name}", async Task<IResult> (string name, IBackupService backupService, CancellationToken cancellationToken) =>
{
    if (/*some condition*/)
    {
        return TypedResults.BadRequest();
    }

    if (/*other condition*/)
    {
        return TypedResults.Ok();
    }

    return TypedResults.Created();
    
})
    .WithName("CheckBackups")
    .WithOpenApi()
    .Produces(StatusCodes.Status200OK)
    .Produces(StatusCodes.Status201Created)
    .Produces(StatusCodes.Status404NotFound);

This generates the below swagger.json file:

{
  "openapi": "3.0.1",
  "info": {
    "title": "TestAPI",
    "version": "1.0"
  },
  "paths": {
    "/check/{name}": {
      "get": {
        "tags": [
          "TestAPI"
        ],
        "operationId": "CheckBackups",
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "style": "simple",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          },
          "201": {
            "description": "Created"
          },
          "404": {
            "description": "Not Found"
          }
        }
      }
    }
  },
  "components": { }
}

What I'm trying to do it to change description in responses, so instead of OK I'd like to have a custom phrase.
I've looked at OpenApiRouteHandlerBuilderExtensions, but I see no method that has this functionality.

This is the outtput that I want to get for this method:

{
  "openapi": "3.0.1",
  "info": {
    "title": "TestAPI",
    "version": "1.0"
  },
  "paths": {
    "/check/{name}": {
      "get": {
        "tags": [
          "TestAPI"
        ],
        "operationId": "CheckBackups",
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "style": "simple",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Backup exists"
          },
          "201": {
            "description": "Backup created"
          },
          "404": {
            "description": "Backup not found"
          }
        }
      }
    }
  },
  "components": { }
}

Of course the new description should be set in code, not hardcoded via OperationFilter.

Any ideas how to do this?

1

There are 1 best solutions below

0
On

I was able to get the expected results with the help of this comment: https://github.com/dotnet/aspnetcore/issues/45192#issuecomment-1322344469

using this code:

.WithOpenApi(operation =>
    {
        operation.Responses["404"].Description = "Backup not found";
        operation.Responses["201"].Description = "Backup created";
        operation.Responses["200"].Description = "Everything is fine";

        return operation;
    }
)

The important thing is that status codes must be added with Produces.