英文:
.NET 7/8 How do I provide a description/summary on a Group with Minimap APIs?
问题
我正在添加一组最小的API,如下所示:
var myapi = app.MapGroup("myapi").WithTags("My API").WithOpenApi();
// and then
myapi.MapGet("/", GetAllStuff).WithName("GetAllStuff");
myapi.MapGet("/{id}", GetSomeStuff).WithName("GetSomeStuff");
这样可以为我提供Swagger UI,但我目前无法为这两个API添加描述,换句话说,无法为该组添加摘要。有什么建议吗?
英文:
I am adding a group of minimal APIs like this:
var myapi = app.MapGroup("myapi").WithTags("My API").WithOpenApi();
// and then
myapi .MapGet($"/", GetAllStuff).WithName("GetAllStuff");
myapi .MapGet($"/{id}", GetSomeStuff).WithName("GetSomeStuff");
This gives me the Swagger UI, but I am currently not able to have a description for both the APIs, in other words, a summary of the Group. Any ideas?
答案1
得分: 1
我看不到一种内置的方式(就像端点的`WithDescription`一样)来提供这个数据,但你可以使用[文档过滤器][1]来解决:
```csharp
public class TagDescriptionsDocumentFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.Tags = new List<OpenApiTag>
{
new OpenApiTag { Name = "我的 API", Description = "我的 API 做了某些事情" }
};
}
}
以及
builder.Services.AddSwaggerGen(opts => opts.DocumentFilter<TagDescriptionsDocumentFilter>());
显然,标签名称必须匹配。
<details>
<summary>英文:</summary>
I don't see a build-in way (like `WithDescription` for endpoints) to provide this data but you can workaround with [document filters][1]:
public class TagDescriptionsDocumentFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.Tags = new List<OpenApiTag>
{
new OpenApiTag { Name = "My API", Description = "My API does something" }
};
}
}
And
builder.Services.AddSwaggerGen(opts => opts.DocumentFilter<TagDescriptionsDocumentFilter>());
Obviously tag names must match.
[![enter image description here][2]][2]
[1]: https://github.com/domaindrivendev/Swashbuckle.AspNetCore#document-filters
[2]: https://i.stack.imgur.com/MPBpn.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论