From e2d30c031de47fe4841f244301fdc71cc504ded0 Mon Sep 17 00:00:00 2001 From: xRain Date: Fri, 19 Dec 2025 13:37:58 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9A=90=E8=97=8F=E4=BA=86=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E4=B8=AD=E7=A9=BATag,=20=E7=A6=81=E7=94=A8=E4=BA=86=E6=96=87?= =?UTF-8?q?=E6=A1=A3=E8=BF=9C=E7=A8=8B=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SimApiExtensions.cs | 3 +-- SwaggerFilters/RemoveEmptyTagsFilter.cs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 SwaggerFilters/RemoveEmptyTagsFilter.cs 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