Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d50d32f032 | ||
|
|
8ad43526fc | ||
|
|
e2d30c031d | ||
|
|
9dc4ef7bce | ||
|
|
d5eb7fe081 | ||
|
|
b04a7c86d7 | ||
|
|
8759b593db | ||
|
|
5a46a026a7 | ||
|
|
90124b6e67 |
@@ -10,11 +10,11 @@ jobs:
|
|||||||
name: Publish Project to Nuget
|
name: Publish Project to Nuget
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v4
|
||||||
- name: Setup .NET Core
|
- name: Setup .NET Core
|
||||||
uses: actions/setup-dotnet@v1
|
uses: actions/setup-dotnet@v5
|
||||||
with:
|
with:
|
||||||
dotnet-version: "9.0.305"
|
dotnet-version: "10.x.x"
|
||||||
- name: Publish
|
- name: Publish
|
||||||
run: |
|
run: |
|
||||||
version=`git describe --tags`
|
version=`git describe --tags`
|
||||||
|
|||||||
@@ -46,23 +46,15 @@ public class SimApiBaseResponse(int code = 200, string message = "成功")
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 动态内容分页
|
/// 分页内容返回
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public class SimApiBasePageResponse<T>() : SimApiBaseResponse
|
public class PageResponse<T>
|
||||||
{
|
{
|
||||||
public T? List { get; set; }
|
public T? List { get; set; }
|
||||||
public int Page { get; set; } = 1;
|
public int Page { get; set; } = 1;
|
||||||
public int Count { get; set; } = 20;
|
public int Count { get; set; } = 20;
|
||||||
public int Total { get; set; }
|
public int Total { get; set; }
|
||||||
|
|
||||||
public SimApiBasePageResponse(T list, int page, int count, int total) : this()
|
|
||||||
{
|
|
||||||
List = list;
|
|
||||||
Page = page;
|
|
||||||
Count = count;
|
|
||||||
Total = total;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -4,20 +4,22 @@ using System.Linq;
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
|
using SimApi.Exceptions;
|
||||||
|
|
||||||
namespace SimApi.Helpers;
|
namespace SimApi.Helpers;
|
||||||
|
|
||||||
public class SimApiHttpClient(string? appId, string appKey)
|
public class SimApiHttpClient(string? appId, string appKey, bool debug = false)
|
||||||
{
|
{
|
||||||
|
public string Server { get; init; } = string.Empty;
|
||||||
public string SignName { get; init; } = "sign";
|
public string SignName { get; init; } = "sign";
|
||||||
public string TimestampName { get; init; } = "timestamp";
|
public string TimestampName { get; init; } = "timestamp";
|
||||||
public string NonceName { get; init; } = "nonce";
|
public string NonceName { get; init; } = "nonce";
|
||||||
public string? AppIdName { get; init; } = "appId";
|
public string? AppIdName { get; init; } = "appId";
|
||||||
public string[] SignFields { get; init; } = [];
|
public string[] SignFields { get; init; } = [];
|
||||||
|
|
||||||
|
public T? SignQuery<T>(string url, object? body = null, Dictionary<string, string>? queries = null)
|
||||||
public T? SignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
|
|
||||||
{
|
{
|
||||||
|
url = Server + url;
|
||||||
var queryUrl = SignFields.Aggregate(string.Empty,
|
var queryUrl = SignFields.Aggregate(string.Empty,
|
||||||
(current, signField) => current + $"{signField}={queries?[signField]}&");
|
(current, signField) => current + $"{signField}={queries?[signField]}&");
|
||||||
if (!string.IsNullOrEmpty(AppIdName))
|
if (!string.IsNullOrEmpty(AppIdName))
|
||||||
@@ -35,13 +37,12 @@ public class SimApiHttpClient(string? appId, string appKey)
|
|||||||
.Aggregate(path, (current, q) => current + $"&{q.Key}={q.Value}");
|
.Aggregate(path, (current, q) => current + $"&{q.Key}={q.Value}");
|
||||||
}
|
}
|
||||||
|
|
||||||
var http = new HttpClient();
|
return Query<T>(path, body);
|
||||||
var resp = http.PostAsJsonAsync(path, body).Result;
|
|
||||||
return resp.Content.ReadFromJsonAsync<T>().Result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T? AesQuery<T>(string url, object body)
|
public T? AesQuery<T>(string url, object body)
|
||||||
{
|
{
|
||||||
|
url = Server + url;
|
||||||
if (!string.IsNullOrEmpty(AppIdName))
|
if (!string.IsNullOrEmpty(AppIdName))
|
||||||
{
|
{
|
||||||
url += $"?{AppIdName}={appId}";
|
url += $"?{AppIdName}={appId}";
|
||||||
@@ -51,9 +52,7 @@ public class SimApiHttpClient(string? appId, string appKey)
|
|||||||
{
|
{
|
||||||
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey)
|
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey)
|
||||||
};
|
};
|
||||||
var http = new HttpClient();
|
return Query<T>(url, req);
|
||||||
var resp = http.PostAsJsonAsync(url, req).Result;
|
|
||||||
return resp.Content.ReadFromJsonAsync<T>().Result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T? AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
|
public T? AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
|
||||||
@@ -64,4 +63,27 @@ public class SimApiHttpClient(string? appId, string appKey)
|
|||||||
};
|
};
|
||||||
return SignQuery<T>(url, req, queries);
|
return SignQuery<T>(url, req, queries);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private T? Query<T>(string url, object? req)
|
||||||
|
{
|
||||||
|
var http = new HttpClient();
|
||||||
|
if (debug)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[HTTPCLIENT请求] {url}\n{SimApiUtil.Json(req)}\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
var resp = http.PostAsJsonAsync(url, req).Result;
|
||||||
|
if (debug)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[HTTPCLIENT响应] {resp.Content.ReadAsStringAsync().Result}\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
var res = resp.Content.ReadFromJsonAsync<SimApiBaseResponse<T>>().Result;
|
||||||
|
if (res == null)
|
||||||
|
{
|
||||||
|
throw new SimApiException(500, "请求发生错误");
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.Code != 200 ? throw new SimApiException(res.Code, res.Message) : res.Data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+4
-4
@@ -9,7 +9,7 @@
|
|||||||
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
|
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
|
||||||
<PackageId>Simcu.SimApi</PackageId>
|
<PackageId>Simcu.SimApi</PackageId>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
|
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -20,11 +20,11 @@
|
|||||||
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.22" />
|
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.22" />
|
||||||
<PackageReference Include="Hangfire.Console" Version="1.4.3"/>
|
<PackageReference Include="Hangfire.Console" Version="1.4.3"/>
|
||||||
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.12.0"/>
|
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.12.0"/>
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.10" />
|
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="10.0.1" />
|
||||||
<PackageReference Include="Minio" Version="7.0.0" />
|
<PackageReference Include="Minio" Version="7.0.0" />
|
||||||
<PackageReference Include="MQTTnet" Version="5.0.1.1416"/>
|
<PackageReference Include="MQTTnet" Version="5.0.1.1416"/>
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="9.0.6" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="10.1.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="9.0.6" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.1.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ProjectExtensions>
|
<ProjectExtensions>
|
||||||
<MonoDevelop>
|
<MonoDevelop>
|
||||||
|
|||||||
+42
-5
@@ -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;
|
||||||
@@ -9,7 +10,7 @@ using Hangfire.Redis.StackExchange;
|
|||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi;
|
||||||
using SimApi.Middlewares;
|
using SimApi.Middlewares;
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@@ -123,11 +124,47 @@ 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<SimApiResponseOperationFilter>();
|
||||||
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>();
|
||||||
@@ -157,11 +194,12 @@ public static class SimApiExtensions
|
|||||||
switch (auth)
|
switch (auth)
|
||||||
{
|
{
|
||||||
case "SimApiAuth":
|
case "SimApiAuth":
|
||||||
x.AddSecurityDefinition("HeaderToken",
|
x.AddSecurityDefinition("SimApiAuth",
|
||||||
new OpenApiSecurityScheme
|
new OpenApiSecurityScheme
|
||||||
{
|
{
|
||||||
Name = "Token",
|
Name = "Token",
|
||||||
In = ParameterLocation.Header
|
In = ParameterLocation.Header,
|
||||||
|
Type = SecuritySchemeType.ApiKey
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "ClientCredentials":
|
case "ClientCredentials":
|
||||||
@@ -392,7 +430,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();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.OpenApi;
|
||||||
using SimApi.Attributes;
|
using SimApi.Attributes;
|
||||||
|
|
||||||
namespace SimApi.SwaggerFilters;
|
namespace SimApi.SwaggerFilters;
|
||||||
|
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,26 @@
|
|||||||
using System;
|
using System;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Text.Json.Nodes;
|
||||||
using Microsoft.OpenApi.Any;
|
|
||||||
|
|
||||||
namespace SimApi.SwaggerFilters;
|
namespace SimApi.SwaggerFilters;
|
||||||
|
|
||||||
public class GlobalDynamicObjectSchemaFilter : ISchemaFilter
|
public class GlobalDynamicObjectSchemaFilter : ISchemaFilter
|
||||||
{
|
{
|
||||||
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
|
public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
|
||||||
{
|
{
|
||||||
if (!IsDynamicObjectType(context.Type)) return;
|
if (!IsDynamicObjectType(context.Type)) return;
|
||||||
schema.AdditionalPropertiesAllowed = true;
|
var oaSchema = schema as OpenApiSchema;
|
||||||
schema.AdditionalProperties = new OpenApiSchema
|
oaSchema.AdditionalPropertiesAllowed = true;
|
||||||
|
oaSchema.AdditionalProperties = new OpenApiSchema
|
||||||
{
|
{
|
||||||
Type = "object", // 表示 value 可以是任意类型(兼容所有类型)
|
Type = JsonSchemaType.Object, // 表示 value 可以是任意类型(兼容所有类型)
|
||||||
Nullable = true
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 2. 覆盖默认示例,使用包含多种类型的示例
|
// 2. 覆盖默认示例,使用包含多种类型的示例
|
||||||
schema.Example = CreateMultiTypeExample();
|
oaSchema.Example = CreateMultiTypeExample();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断是否为需要处理的“动态对象”类型
|
// 判断是否为需要处理的“动态对象”类型
|
||||||
@@ -33,30 +32,39 @@ public class GlobalDynamicObjectSchemaFilter : ISchemaFilter
|
|||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == typeof(object))
|
if (type == typeof(object))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return type.Name.Contains("AnonymousType") && type.Namespace == null;
|
return type.Name.Contains("AnonymousType") && type.Namespace == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建包含多种类型的示例(覆盖默认的 string 示例)
|
// 创建包含多种类型的示例(覆盖默认的 string 示例)
|
||||||
private OpenApiObject CreateMultiTypeExample()
|
private JsonNode CreateMultiTypeExample()
|
||||||
{
|
{
|
||||||
return new OpenApiObject
|
return new JsonObject
|
||||||
{
|
{
|
||||||
["stringProp"] = new OpenApiString("example string"), // 字符串
|
// 字符串类型:JsonValue.Create 包装字符串
|
||||||
["numberProp"] = new OpenApiInteger(123), // 数字
|
["stringProp"] = JsonValue.Create("example string"),
|
||||||
["boolProp"] = new OpenApiBoolean(true), // 布尔值
|
// 数字类型:支持 int/long/double 等,JsonValue 自动适配
|
||||||
["objectProp"] = new OpenApiObject // 嵌套对象
|
["numberProp"] = JsonValue.Create(123),
|
||||||
|
// 布尔类型
|
||||||
|
["boolProp"] = JsonValue.Create(true),
|
||||||
|
// 嵌套对象:JsonObject 对应 OpenApiObject
|
||||||
|
["objectProp"] = new JsonObject
|
||||||
{
|
{
|
||||||
["nestedKey"] = new OpenApiString("nested value")
|
["nestedKey"] = JsonValue.Create("nested value")
|
||||||
},
|
},
|
||||||
["arrayProp"] = new OpenApiArray // 数组
|
// 数组类型:JsonArray 对应 OpenApiArray
|
||||||
|
["arrayProp"] = new JsonArray
|
||||||
{
|
{
|
||||||
new OpenApiInteger(1),
|
JsonValue.Create(1), // 数组内数字
|
||||||
new OpenApiString("two")
|
JsonValue.Create("two") // 数组内字符串
|
||||||
}
|
},
|
||||||
|
// 可选:添加 null 值示例(若需要)
|
||||||
|
["nullProp"] = null
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
using SimApi.Attributes;
|
using SimApi.Attributes;
|
||||||
|
|
||||||
@@ -18,28 +18,12 @@ namespace SimApi.SwaggerFilters
|
|||||||
||
|
||
|
||||||
// 控制器上有 [SimApiAuth](继承到所有方法)
|
// 控制器上有 [SimApiAuth](继承到所有方法)
|
||||||
context.MethodInfo.DeclaringType?.GetCustomAttributes<SimApiAuthAttribute>(true).Any() == true;
|
context.MethodInfo.DeclaringType?.GetCustomAttributes<SimApiAuthAttribute>(true).Any() == true;
|
||||||
if (requiresAuth)
|
if (!requiresAuth) return;
|
||||||
|
operation.Security ??= new List<OpenApiSecurityRequirement>();
|
||||||
|
operation.Security.Add(new OpenApiSecurityRequirement()
|
||||||
{
|
{
|
||||||
// 添加授权要求:关联步骤 2 中定义的 "SimApiAuth" 安全方案
|
{ new OpenApiSecuritySchemeReference("SimApiAuth", context.Document), [] }
|
||||||
operation.Security = new List<OpenApiSecurityRequirement>
|
});
|
||||||
{
|
|
||||||
new OpenApiSecurityRequirement
|
|
||||||
{
|
|
||||||
{
|
|
||||||
new OpenApiSecurityScheme
|
|
||||||
{
|
|
||||||
Reference = new OpenApiReference
|
|
||||||
{
|
|
||||||
Type = ReferenceType.SecurityScheme,
|
|
||||||
Id = "SimApiAuth" // 必须与 AddSecurityDefinition 的第一个参数一致
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[] // 无需指定作用域(scope)时留空
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
// 未标记 [SimApiAuth] 的接口:不添加安全要求,Swagger 不显示锁图标
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi;
|
||||||
using SimApi.Attributes;
|
using SimApi.Attributes;
|
||||||
using SimApi.ModelBinders;
|
using SimApi.ModelBinders;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
@@ -67,20 +67,24 @@ public class SimApiSignOperationFilter(IServiceProvider serviceProvider) : IOper
|
|||||||
signParameters.AddRange(keyProvider.SignFields.Where(x => x != keyProvider.AppIdName)
|
signParameters.AddRange(keyProvider.SignFields.Where(x => x != keyProvider.AppIdName)
|
||||||
.Select(f => (f, string.Empty, true)));
|
.Select(f => (f, string.Empty, true)));
|
||||||
|
|
||||||
|
operation.Parameters ??= new List<IOpenApiParameter>();
|
||||||
|
|
||||||
foreach (var (name, description, required) in signParameters)
|
foreach (var (name, description, required) in signParameters)
|
||||||
{
|
{
|
||||||
if (operation.Parameters?.Any(p => p.Name == name) == true)
|
if (operation.Parameters?.Any(p => p.Name == name) == true)
|
||||||
{
|
{
|
||||||
var tmp = operation.Parameters?.FirstOrDefault(p => p.Name == name);
|
var tmp = operation.Parameters?.FirstOrDefault(p => p.Name == name);
|
||||||
if (tmp != null)
|
if (tmp is OpenApiParameter concreteParam)
|
||||||
{
|
{
|
||||||
tmp.Required = required;
|
// 重新赋值只读属性(通过实例化新对象覆盖,或直接修改具体类的可写属性)
|
||||||
tmp.Description = description;
|
concreteParam.Required = required; // OpenApiParameter 的 Required 有 setter
|
||||||
|
concreteParam.Description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
operation.Parameters ??= new List<OpenApiParameter>();
|
operation.Parameters ??= new List<IOpenApiParameter>();
|
||||||
operation.Parameters.Add(new OpenApiParameter
|
operation.Parameters.Add(new OpenApiParameter
|
||||||
{
|
{
|
||||||
Name = name,
|
Name = name,
|
||||||
@@ -89,7 +93,7 @@ public class SimApiSignOperationFilter(IServiceProvider serviceProvider) : IOper
|
|||||||
Required = required,
|
Required = required,
|
||||||
Schema = new OpenApiSchema
|
Schema = new OpenApiSchema
|
||||||
{
|
{
|
||||||
Type = "string" // 签名相关参数通常为字符串类型
|
Type = JsonSchemaType.String // 签名相关参数通常为字符串类型
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user