.NET 7/8如何在Minimap APIs中为分组提供描述/摘要?

huangapple go评论165阅读模式
英文:

.NET 7/8 How do I provide a description/summary on a Group with Minimap APIs?

问题

我正在添加一组最小的API,如下所示:

  1. var myapi = app.MapGroup("myapi").WithTags("My API").WithOpenApi();
  2. // and then
  3. myapi.MapGet("/", GetAllStuff).WithName("GetAllStuff");
  4. myapi.MapGet("/{id}", GetSomeStuff).WithName("GetSomeStuff");

这样可以为我提供Swagger UI,但我目前无法为这两个API添加描述,换句话说,无法为该组添加摘要。有什么建议吗?

英文:

I am adding a group of minimal APIs like this:

  1. var myapi = app.MapGroup("myapi").WithTags("My API").WithOpenApi();
  2. // and then
  3. myapi .MapGet($"/", GetAllStuff).WithName("GetAllStuff");
  4. 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

  1. 我看不到一种内置的方式(就像端点的`WithDescription`一样)来提供这个数据,但你可以使用[文档过滤器][1]来解决:
  2. ```csharp
  3. public class TagDescriptionsDocumentFilter : IDocumentFilter
  4. {
  5. public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
  6. {
  7. swaggerDoc.Tags = new List<OpenApiTag>
  8. {
  9. new OpenApiTag { Name = "我的 API", Description = "我的 API 做了某些事情" }
  10. };
  11. }
  12. }

以及

  1. builder.Services.AddSwaggerGen(opts => opts.DocumentFilter<TagDescriptionsDocumentFilter>());

显然,标签名称必须匹配。

.NET 7/8如何在Minimap APIs中为分组提供描述/摘要?

  1. <details>
  2. <summary>英文:</summary>
  3. I don&#39;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" }
};
}
}

  1. And

builder.Services.AddSwaggerGen(opts => opts.DocumentFilter<TagDescriptionsDocumentFilter>());

  1. Obviously tag names must match.
  2. [![enter image description here][2]][2]
  3. [1]: https://github.com/domaindrivendev/Swashbuckle.AspNetCore#document-filters
  4. [2]: https://i.stack.imgur.com/MPBpn.png
  5. </details>

huangapple
  • 本文由 发表于 2023年8月10日 22:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定