'Add Swagger description to minimal .NET6 APIs
I have a small project in .NET6 that contains minimal APIs like that one
app.MapGet("/clients",
async (IClientRepository repo) =>
{
var results = await repo.GetClientsAsync();
return mapper.Map<IEnumerable<ClientModel>>(results);
});
In the SwaggerUI
I can use this API but I can't find a way to add description to it (although in the project settings I check for creating an API XML documentation).
How can I add the XML comment?
Solution 1:[1]
Currently support for Open API docs for minimal APIs is quite minimal and does not allow adding descriptions/summaries as far as I can see. There is a feature planned for .NET 7 to add descriptions. Also soon Swashbuckle
should consider EndpointMetadata
for annotations.
Also related issue.
Solution 2:[2]
package Swashbuckle.AspNetCore.Annotations
6.3
...
builder.Services.AddSwaggerGen(c => c.EnableAnnotations());
var app = builder.build();
app.MapGet("/clients",
[SwaggerOperation(
Summary = "returns clients",
Description = "more description on get `clients`")]
[SwaggerResponse(200, "success")]
[SwaggerResponse(500, "some failure")]
async (IClientRepository repo) =>
{
var results = await repo.GetClientsAsync();
return mapper.Map<IEnumerable<ClientModel>>(results);
}).WithTags("Clients");
more examples here https://github.com/domaindrivendev/Swashbuckle.AspNetCore#enrich-operation-metadata
Solution 3:[3]
You may use this guide. It worked for me using Swashbuckle. There are extension methods that come with minimal APIs. Here is how it looks:
app.MapGet(“/books”, async (BooksDB db) =>
await db.Books.ToListAsync()
)
.Produces<List<Book>>(StatusCodes.Status200OK)
.WithName(“GetAllBooks”).WithTags(“Getters”);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | daggett |
Solution 3 | Andrey R |