Skip to content

Commit 90124b6

Browse files
committed
update v10
1 parent 2499912 commit 90124b6

7 files changed

Lines changed: 63 additions & 54 deletions

SimApi.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
1010
<PackageId>Simcu.SimApi</PackageId>
1111
<Nullable>enable</Nullable>
12-
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
12+
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
1313
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1414
</PropertyGroup>
1515
<ItemGroup>
@@ -20,11 +20,11 @@
2020
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.22" />
2121
<PackageReference Include="Hangfire.Console" Version="1.4.3"/>
2222
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.12.0"/>
23-
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.10" />
23+
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="10.0.1" />
2424
<PackageReference Include="Minio" Version="7.0.0" />
2525
<PackageReference Include="MQTTnet" Version="5.0.1.1416"/>
26-
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="9.0.6" />
27-
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="9.0.6" />
26+
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="10.0.1" />
27+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.0.1" />
2828
</ItemGroup>
2929
<ProjectExtensions>
3030
<MonoDevelop>

SimApiExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using SimApi.Helpers;
1010
using Microsoft.AspNetCore.Builder;
1111
using Microsoft.Extensions.DependencyInjection;
12-
using Microsoft.OpenApi.Models;
12+
using Microsoft.OpenApi;
1313
using SimApi.Middlewares;
1414
using Microsoft.AspNetCore.HttpOverrides;
1515
using Microsoft.AspNetCore.Mvc;

SwaggerFilters/AesBodyOperationFilter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using Microsoft.OpenApi;
34
using SimApi.Attributes;
45

56
namespace SimApi.SwaggerFilters;
67

7-
using Microsoft.OpenApi.Models;
8+
using Microsoft.OpenApi;
89
using Swashbuckle.AspNetCore.SwaggerGen;
910
using System.Reflection;
1011

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
using System;
2-
using Microsoft.OpenApi.Models;
2+
using Microsoft.OpenApi;
33
using Swashbuckle.AspNetCore.SwaggerGen;
44
using System.Collections;
55
using System.Collections.Generic;
6-
using System.Reflection;
7-
using Microsoft.OpenApi.Any;
6+
using System.Text.Json.Nodes;
87

98
namespace SimApi.SwaggerFilters;
109

1110
public class GlobalDynamicObjectSchemaFilter : ISchemaFilter
1211
{
13-
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
12+
public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
1413
{
1514
if (!IsDynamicObjectType(context.Type)) return;
16-
schema.AdditionalPropertiesAllowed = true;
17-
schema.AdditionalProperties = new OpenApiSchema
15+
var oaSchema = schema as OpenApiSchema;
16+
oaSchema.AdditionalPropertiesAllowed = true;
17+
oaSchema.AdditionalProperties = new OpenApiSchema
1818
{
19-
Type = "object", // 表示 value 可以是任意类型(兼容所有类型)
20-
Nullable = true
19+
Type = JsonSchemaType.Object, // 表示 value 可以是任意类型(兼容所有类型)
2120
};
2221

2322
// 2. 覆盖默认示例,使用包含多种类型的示例
24-
schema.Example = CreateMultiTypeExample();
23+
oaSchema.Example = CreateMultiTypeExample();
2524
}
2625

2726
// 判断是否为需要处理的“动态对象”类型
@@ -33,30 +32,39 @@ private bool IsDynamicObjectType(Type? type)
3332
{
3433
return true;
3534
}
35+
3636
if (type == typeof(object))
3737
{
3838
return true;
3939
}
40+
4041
return type.Name.Contains("AnonymousType") && type.Namespace == null;
4142
}
4243

4344
// 创建包含多种类型的示例(覆盖默认的 string 示例)
44-
private OpenApiObject CreateMultiTypeExample()
45+
private JsonNode CreateMultiTypeExample()
4546
{
46-
return new OpenApiObject
47+
return new JsonObject
4748
{
48-
["stringProp"] = new OpenApiString("example string"), // 字符串
49-
["numberProp"] = new OpenApiInteger(123), // 数字
50-
["boolProp"] = new OpenApiBoolean(true), // 布尔值
51-
["objectProp"] = new OpenApiObject // 嵌套对象
49+
// 字符串类型:JsonValue.Create 包装字符串
50+
["stringProp"] = JsonValue.Create("example string"),
51+
// 数字类型:支持 int/long/double 等,JsonValue 自动适配
52+
["numberProp"] = JsonValue.Create(123),
53+
// 布尔类型
54+
["boolProp"] = JsonValue.Create(true),
55+
// 嵌套对象:JsonObject 对应 OpenApiObject
56+
["objectProp"] = new JsonObject
5257
{
53-
["nestedKey"] = new OpenApiString("nested value")
58+
["nestedKey"] = JsonValue.Create("nested value")
5459
},
55-
["arrayProp"] = new OpenApiArray // 数组
60+
// 数组类型:JsonArray 对应 OpenApiArray
61+
["arrayProp"] = new JsonArray
5662
{
57-
new OpenApiInteger(1),
58-
new OpenApiString("two")
59-
}
63+
JsonValue.Create(1), // 数组内数字
64+
JsonValue.Create("two") // 数组内字符串
65+
},
66+
// 可选:添加 null 值示例(若需要)
67+
["nullProp"] = null
6068
};
6169
}
6270
}
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using System.Reflection;
4-
using Microsoft.OpenApi.Models;
4+
using Microsoft.OpenApi;
55
using Swashbuckle.AspNetCore.SwaggerGen;
66
using SimApi.Attributes;
77

@@ -18,28 +18,24 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
1818
||
1919
// 控制器上有 [SimApiAuth](继承到所有方法)
2020
context.MethodInfo.DeclaringType?.GetCustomAttributes<SimApiAuthAttribute>(true).Any() == true;
21-
if (requiresAuth)
21+
if (!requiresAuth) return;
22+
23+
// 关键修复:正确构造 OpenApiSecuritySchemeReference(匹配键类型要求)
24+
var securitySchemeRef = new OpenApiSecuritySchemeReference(
25+
referenceId: "SimApiAuth", // 必须与 AddSecurityDefinition 的 ID 一致
26+
hostDocument: null,
27+
externalResource: null
28+
);
29+
30+
// 给 Security 赋值(确保键类型是 OpenApiSecuritySchemeReference)
31+
operation.Security ??= new List<OpenApiSecurityRequirement>();
32+
operation.Security.Add(new OpenApiSecurityRequirement
2233
{
23-
// 添加授权要求:关联步骤 2 中定义的 "SimApiAuth" 安全方案
24-
operation.Security = new List<OpenApiSecurityRequirement>
2534
{
26-
new OpenApiSecurityRequirement
27-
{
28-
{
29-
new OpenApiSecurityScheme
30-
{
31-
Reference = new OpenApiReference
32-
{
33-
Type = ReferenceType.SecurityScheme,
34-
Id = "SimApiAuth" // 必须与 AddSecurityDefinition 的第一个参数一致
35-
}
36-
},
37-
[] // 无需指定作用域(scope)时留空
38-
}
39-
}
40-
};
41-
}
42-
// 未标记 [SimApiAuth] 的接口:不添加安全要求,Swagger 不显示锁图标
35+
securitySchemeRef,
36+
[]
37+
}
38+
});
4339
}
4440
}
4541
}

SwaggerFilters/SimApiResponseOperationFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using Microsoft.OpenApi.Models;
4+
using Microsoft.OpenApi;
55
using Swashbuckle.AspNetCore.SwaggerGen;
66
using System.Reflection;
77
using System.Threading.Tasks;

SwaggerFilters/SimApiSignOperationFilter.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Linq;
44
using System.Reflection;
55
using Microsoft.Extensions.DependencyInjection;
6-
using Microsoft.OpenApi.Models;
6+
using Microsoft.OpenApi;
77
using SimApi.Attributes;
88
using SimApi.ModelBinders;
99
using Swashbuckle.AspNetCore.SwaggerGen;
@@ -67,20 +67,24 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
6767
signParameters.AddRange(keyProvider.SignFields.Where(x => x != keyProvider.AppIdName)
6868
.Select(f => (f, string.Empty, true)));
6969

70+
operation.Parameters ??= new List<IOpenApiParameter>();
71+
7072
foreach (var (name, description, required) in signParameters)
7173
{
7274
if (operation.Parameters?.Any(p => p.Name == name) == true)
7375
{
7476
var tmp = operation.Parameters?.FirstOrDefault(p => p.Name == name);
75-
if (tmp != null)
77+
if (tmp is OpenApiParameter concreteParam)
7678
{
77-
tmp.Required = required;
78-
tmp.Description = description;
79+
// 重新赋值只读属性(通过实例化新对象覆盖,或直接修改具体类的可写属性)
80+
concreteParam.Required = required; // OpenApiParameter 的 Required 有 setter
81+
concreteParam.Description = description;
7982
}
83+
8084
continue;
8185
}
8286

83-
operation.Parameters ??= new List<OpenApiParameter>();
87+
operation.Parameters ??= new List<IOpenApiParameter>();
8488
operation.Parameters.Add(new OpenApiParameter
8589
{
8690
Name = name,
@@ -89,7 +93,7 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
8993
Required = required,
9094
Schema = new OpenApiSchema
9195
{
92-
Type = "string" // 签名相关参数通常为字符串类型
96+
Type = JsonSchemaType.String // 签名相关参数通常为字符串类型
9397
}
9498
});
9599
}

0 commit comments

Comments
 (0)