Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ad43526fc | ||
|
|
e2d30c031d |
+16
-2
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@@ -128,6 +129,7 @@ public static class SimApiExtensions
|
|||||||
x.OperationFilter<SimApiSignOperationFilter>();
|
x.OperationFilter<SimApiSignOperationFilter>();
|
||||||
x.OperationFilter<AesBodyOperationFilter>();
|
x.OperationFilter<AesBodyOperationFilter>();
|
||||||
x.SchemaFilter<GlobalDynamicObjectSchemaFilter>();
|
x.SchemaFilter<GlobalDynamicObjectSchemaFilter>();
|
||||||
|
x.DocumentFilter<RemoveEmptyTagsFilter>();
|
||||||
if (simApiOptions.EnableSimApiAuth)
|
if (simApiOptions.EnableSimApiAuth)
|
||||||
{
|
{
|
||||||
x.OperationFilter<SimApiAuthOperationFilter>();
|
x.OperationFilter<SimApiAuthOperationFilter>();
|
||||||
@@ -161,7 +163,14 @@ public static class SimApiExtensions
|
|||||||
new OpenApiSecurityScheme
|
new OpenApiSecurityScheme
|
||||||
{
|
{
|
||||||
Name = "Token",
|
Name = "Token",
|
||||||
In = ParameterLocation.Header
|
In = ParameterLocation.Header,
|
||||||
|
Type = SecuritySchemeType.ApiKey
|
||||||
|
});
|
||||||
|
x.AddSecurityRequirement(docs => new OpenApiSecurityRequirement
|
||||||
|
{
|
||||||
|
{
|
||||||
|
new OpenApiSecuritySchemeReference("HeaderToken"), new List<string>()
|
||||||
|
}
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "ClientCredentials":
|
case "ClientCredentials":
|
||||||
@@ -211,6 +220,12 @@ public static class SimApiExtensions
|
|||||||
Description = docOptions.ApiAuth.Description,
|
Description = docOptions.ApiAuth.Description,
|
||||||
In = ParameterLocation.Header
|
In = ParameterLocation.Header
|
||||||
});
|
});
|
||||||
|
x.AddSecurityRequirement(docs => new OpenApiSecurityRequirement
|
||||||
|
{
|
||||||
|
{
|
||||||
|
new OpenApiSecuritySchemeReference("oauth2"), new List<string>()
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -392,7 +407,6 @@ public static class SimApiExtensions
|
|||||||
x.SwaggerEndpoint($"/swagger/{group.Id}.json", name: group.Name);
|
x.SwaggerEndpoint($"/swagger/{group.Id}.json", name: group.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
x.EnableValidator();
|
|
||||||
x.SupportedSubmitMethods(docOptions.SupportedMethod);
|
x.SupportedSubmitMethods(docOptions.SupportedMethod);
|
||||||
x.DisplayRequestDuration();
|
x.DisplayRequestDuration();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user