diff --git a/src/SharedKernel/OpenApi/OpenApiExtensions.cs b/src/SharedKernel/OpenApi/OpenApiExtensions.cs index 95e32a7..f487fa8 100644 --- a/src/SharedKernel/OpenApi/OpenApiExtensions.cs +++ b/src/SharedKernel/OpenApi/OpenApiExtensions.cs @@ -33,6 +33,7 @@ public static WebApplicationBuilder AddOpenApi(this WebApplicationBuilder builde options.AddDocument(document, openApiConfiguration); options.AddSchemaTransformer(); options.UseApiSecuritySchemes(openApiConfiguration); + options.AddDocumentTransformer(); configureOptions?.Invoke(options); }); } diff --git a/src/SharedKernel/OpenApi/TagOrderingTransformer.cs b/src/SharedKernel/OpenApi/TagOrderingTransformer.cs new file mode 100644 index 0000000..acb4dbd --- /dev/null +++ b/src/SharedKernel/OpenApi/TagOrderingTransformer.cs @@ -0,0 +1,52 @@ +using Microsoft.AspNetCore.OpenApi; +using Microsoft.OpenApi.Models; + +namespace SharedKernel.OpenApi; + +internal sealed class TagOrderingTransformer : IOpenApiDocumentTransformer +{ + public Task TransformAsync(OpenApiDocument document, + OpenApiDocumentTransformerContext context, + CancellationToken cancellationToken) + { + var tags = new HashSet(StringComparer.OrdinalIgnoreCase); + foreach (var path in document.Paths.Values) + { + foreach (var op in path.Operations.Values) + { + foreach (var t in op.Tags ?? []) + { + tags.Add(t.Name); + } + } + } + + var ordered = tags.OrderBy(t => t, StringComparer.OrdinalIgnoreCase) + .ToList(); + + document.Tags = ordered.Select(t => new OpenApiTag + { + Name = t + }) + .ToList(); + + var index = document.Tags + .Select((t, i) => (t.Name, i)) + .ToDictionary(x => x.Name, x => x.i, StringComparer.OrdinalIgnoreCase); + + foreach (var path in document.Paths.Values) + { + foreach (var op in path.Operations.Values) + { + if (op.Tags is { Count: > 1 }) + { + op.Tags = op.Tags + .OrderBy(t => index[t.Name]) + .ToList(); + } + } + } + + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/src/SharedKernel/OpenApi/UiExtensions.cs b/src/SharedKernel/OpenApi/UiExtensions.cs index 2b2cbda..f825579 100644 --- a/src/SharedKernel/OpenApi/UiExtensions.cs +++ b/src/SharedKernel/OpenApi/UiExtensions.cs @@ -40,6 +40,7 @@ internal static WebApplication MapScalarUi(this WebApplication app, OpenApiConfi { options.Theme = ScalarTheme.Kepler; options.Favicon = "/swagger-resources/favicon.svg"; + options.SortTagsAlphabetically(); foreach (var document in openApiConfigConfiguration.Documents) { diff --git a/src/SharedKernel/SharedKernel.csproj b/src/SharedKernel/SharedKernel.csproj index c594d5b..cd22f5b 100644 --- a/src/SharedKernel/SharedKernel.csproj +++ b/src/SharedKernel/SharedKernel.csproj @@ -14,51 +14,50 @@ Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document. https://github.com/PandaTechAM/be-lib-sharedkernel - nuget updates + tag ordering added to open api - - + + - + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + +