优化了文档中的类型名称,修复了认证接口不显示需要认证
This commit is contained in:
+2
-2
@@ -23,8 +23,8 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="10.0.1" />
|
||||
<PackageReference Include="Minio" Version="7.0.0" />
|
||||
<PackageReference Include="MQTTnet" Version="5.0.1.1416"/>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="10.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="10.1.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.1.0" />
|
||||
</ItemGroup>
|
||||
<ProjectExtensions>
|
||||
<MonoDevelop>
|
||||
|
||||
+37
-14
@@ -124,7 +124,42 @@ public static class SimApiExtensions
|
||||
});
|
||||
}
|
||||
|
||||
x.CustomSchemaIds(type => type.FullName?.Replace("+", "."));
|
||||
x.CustomSchemaIds(type =>
|
||||
{
|
||||
string GetSimpleTypeName(Type t)
|
||||
{
|
||||
if (t.IsArray)
|
||||
{
|
||||
var elementType = t.GetElementType();
|
||||
return $"{GetSimpleTypeName(elementType)}[]";
|
||||
}
|
||||
|
||||
if (t.IsPrimitive || t == typeof(string) || t == typeof(DateTime) || t == typeof(Guid))
|
||||
{
|
||||
return t.Name switch
|
||||
{
|
||||
"String" => "string",
|
||||
"Int32" => "int",
|
||||
"Int64" => "long",
|
||||
"Boolean" => "boolean",
|
||||
"DateTime" => "DateTime",
|
||||
"Guid" => "Guid",
|
||||
_ => t.Name
|
||||
};
|
||||
}
|
||||
|
||||
return t.Name;
|
||||
}
|
||||
|
||||
if (!type.IsGenericType) return GetSimpleTypeName(type);
|
||||
var baseName = type.GetGenericTypeDefinition().Name.Split('`')[0];
|
||||
var genericArgs = type.GetGenericArguments()
|
||||
.Select(GetSimpleTypeName)
|
||||
.ToArray();
|
||||
return genericArgs.Length > 0
|
||||
? $"{baseName}<{string.Join(",", genericArgs)}>"
|
||||
: baseName;
|
||||
});
|
||||
x.OperationFilter<SimApiResponseOperationFilter>();
|
||||
x.OperationFilter<SimApiSignOperationFilter>();
|
||||
x.OperationFilter<AesBodyOperationFilter>();
|
||||
@@ -159,19 +194,13 @@ public static class SimApiExtensions
|
||||
switch (auth)
|
||||
{
|
||||
case "SimApiAuth":
|
||||
x.AddSecurityDefinition("HeaderToken",
|
||||
x.AddSecurityDefinition("SimApiAuth",
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Name = "Token",
|
||||
In = ParameterLocation.Header,
|
||||
Type = SecuritySchemeType.ApiKey
|
||||
});
|
||||
x.AddSecurityRequirement(docs => new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecuritySchemeReference("HeaderToken"), new List<string>()
|
||||
}
|
||||
});
|
||||
break;
|
||||
case "ClientCredentials":
|
||||
oauthFlows.ClientCredentials = new OpenApiOAuthFlow
|
||||
@@ -220,12 +249,6 @@ public static class SimApiExtensions
|
||||
Description = docOptions.ApiAuth.Description,
|
||||
In = ParameterLocation.Header
|
||||
});
|
||||
x.AddSecurityRequirement(docs => new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecuritySchemeReference("oauth2"), new List<string>()
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -19,22 +19,10 @@ namespace SimApi.SwaggerFilters
|
||||
// 控制器上有 [SimApiAuth](继承到所有方法)
|
||||
context.MethodInfo.DeclaringType?.GetCustomAttributes<SimApiAuthAttribute>(true).Any() == true;
|
||||
if (!requiresAuth) return;
|
||||
|
||||
// 关键修复:正确构造 OpenApiSecuritySchemeReference(匹配键类型要求)
|
||||
var securitySchemeRef = new OpenApiSecuritySchemeReference(
|
||||
referenceId: "SimApiAuth", // 必须与 AddSecurityDefinition 的 ID 一致
|
||||
hostDocument: null,
|
||||
externalResource: null
|
||||
);
|
||||
|
||||
// 给 Security 赋值(确保键类型是 OpenApiSecuritySchemeReference)
|
||||
operation.Security ??= new List<OpenApiSecurityRequirement>();
|
||||
operation.Security.Add(new OpenApiSecurityRequirement
|
||||
operation.Security.Add(new OpenApiSecurityRequirement()
|
||||
{
|
||||
{
|
||||
securitySchemeRef,
|
||||
[]
|
||||
}
|
||||
{ new OpenApiSecuritySchemeReference("SimApiAuth", context.Document), [] }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user