diff --git a/SimApiExtensions.cs b/SimApiExtensions.cs index 3853b5a..bc313b4 100644 --- a/SimApiExtensions.cs +++ b/SimApiExtensions.cs @@ -122,12 +122,12 @@ public static class SimApiExtensions Description = group.Description }); } - x.CustomSchemaIds(type => type.FullName?.Replace("+", ".")); x.OperationFilter(); x.OperationFilter(); x.OperationFilter(); x.SchemaFilter(); + x.DocumentFilter(); if (simApiOptions.EnableSimApiAuth) { x.OperationFilter(); @@ -392,7 +392,6 @@ public static class SimApiExtensions x.SwaggerEndpoint($"/swagger/{group.Id}.json", name: group.Name); } - x.EnableValidator(); x.SupportedSubmitMethods(docOptions.SupportedMethod); x.DisplayRequestDuration(); }); diff --git a/SwaggerFilters/RemoveEmptyTagsFilter.cs b/SwaggerFilters/RemoveEmptyTagsFilter.cs new file mode 100644 index 0000000..5029f98 --- /dev/null +++ b/SwaggerFilters/RemoveEmptyTagsFilter.cs @@ -0,0 +1,25 @@ +using Microsoft.OpenApi; +using Swashbuckle.AspNetCore.SwaggerGen; +using System.Linq; + +namespace SimApi.SwaggerFilters; + +public class RemoveEmptyTagsFilter : IDocumentFilter +{ + public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) + { + // 步骤1:收集所有有接口的 Tag 名称 + var tagsWithOperations = swaggerDoc.Paths.Values + .SelectMany(path => path.Operations.Values) + .SelectMany(op => op.Tags.Select(t => t.Name)) + .Distinct() + .ToList(); + + // 步骤2:移除无接口的空 Tag + var emptyTags = swaggerDoc.Tags.Where(t => !tagsWithOperations.Contains(t.Name)).ToList(); + foreach (var tag in emptyTags) + { + swaggerDoc.Tags.Remove(tag); + } + } +} \ No newline at end of file