Skip to content

Commit e2d30c0

Browse files
committed
隐藏了文档中空Tag, 禁用了文档远程校验
1 parent 9dc4ef7 commit e2d30c0

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

SimApiExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ public static IServiceCollection AddSimApi(this IServiceCollection builder,
122122
Description = group.Description
123123
});
124124
}
125-
126125
x.CustomSchemaIds(type => type.FullName?.Replace("+", "."));
127126
x.OperationFilter<SimApiResponseOperationFilter>();
128127
x.OperationFilter<SimApiSignOperationFilter>();
129128
x.OperationFilter<AesBodyOperationFilter>();
130129
x.SchemaFilter<GlobalDynamicObjectSchemaFilter>();
130+
x.DocumentFilter<RemoveEmptyTagsFilter>();
131131
if (simApiOptions.EnableSimApiAuth)
132132
{
133133
x.OperationFilter<SimApiAuthOperationFilter>();
@@ -392,7 +392,6 @@ public static WebApplication UseSimApi(this WebApplication builder)
392392
x.SwaggerEndpoint($"/swagger/{group.Id}.json", name: group.Name);
393393
}
394394

395-
x.EnableValidator();
396395
x.SupportedSubmitMethods(docOptions.SupportedMethod);
397396
x.DisplayRequestDuration();
398397
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.OpenApi;
2+
using Swashbuckle.AspNetCore.SwaggerGen;
3+
using System.Linq;
4+
5+
namespace SimApi.SwaggerFilters;
6+
7+
public class RemoveEmptyTagsFilter : IDocumentFilter
8+
{
9+
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
10+
{
11+
// 步骤1:收集所有有接口的 Tag 名称
12+
var tagsWithOperations = swaggerDoc.Paths.Values
13+
.SelectMany(path => path.Operations.Values)
14+
.SelectMany(op => op.Tags.Select(t => t.Name))
15+
.Distinct()
16+
.ToList();
17+
18+
// 步骤2:移除无接口的空 Tag
19+
var emptyTags = swaggerDoc.Tags.Where(t => !tagsWithOperations.Contains(t.Name)).ToList();
20+
foreach (var tag in emptyTags)
21+
{
22+
swaggerDoc.Tags.Remove(tag);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)