ITADN

Ability to set a description for a minimal api group

#249Openhanzllcc 创建于 2025-02-10
feature-request
H
hanzllcccommented
### Is your feature request related to a problem? No It would be useful to have an easy way to add a description to a minimal API group. Currently, this code does not work as expected:: ``` app.MapGroup("products") .WithTags("Products") .WithDescription("List of api products."); ``` As we can see, the description "List of api products." is missing. ![Image](https://github.com/user-attachments/assets/cbdd1e53-8c9a-4c97-8965-e265ada33a3f) ### Describe alternatives you've considered A possible solution is to add an overload of the `WithTags `method that takes an instance of a subclass of OpenApiTag as a parameter. This instance should be read in a document transformer and added to the OpenApiDocument. This is the new extension method `WithTags`: ``` public static class RoutingEndpointConventionBuilderExtensions { public static RouteGroupBuilder WithTags(this RouteGroupBuilder builder, OpenApiDocumentTag tag) => builder.WithTags(tag.Name) .WithMetadata(tag); } public sealed class OpenApiDocumentTag : OpenApiTag; ``` And this is the open api transformer class: ``` public sealed class DocumentTagTransformer : IOpenApiDocumentTransformer { public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken) { var processedTags = new HashSet<string>(); foreach (var documentTag in context.DescriptionGroups .SelectMany(g => g.Items) .SelectMany(m => m.ActionDescriptor.EndpointMetadata) .OfType<OpenApiDocumentTag>()) { if (processedTags.Contains(documentTag.Name)) { continue; } for (var i = 0; i < document.Tags.Count; i++) { if (document.Tags[i].Name == documentTag.Name) { document.Tags.RemoveAt(i); document.Tags.Insert(i, documentTag); processedTags.Add(documentTag.Name); break; } } } return Task.CompletedTask; } } ``` Then, the invocation to the new extension method `WithTags` should be simple. ``` app.MapGroup("/products") .WithTags(new OpenApiDocumentTag { Name = "Products", Description = "List of product APIs." }); ``` Finally, the description is displayed correctly ![Image](https://github.com/user-attachments/assets/9ceeb182-74f7-4a84-afdc-6894434060b7)
1 条评论