Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2499912204 | ||
|
|
dd38f315d5 |
+2
-2
@@ -17,11 +17,11 @@
|
||||
<Folder Include="Exceptions\"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.21" />
|
||||
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.22" />
|
||||
<PackageReference Include="Hangfire.Console" Version="1.4.3"/>
|
||||
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.12.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.10" />
|
||||
<PackageReference Include="Minio" Version="6.0.5" />
|
||||
<PackageReference Include="Minio" Version="7.0.0" />
|
||||
<PackageReference Include="MQTTnet" Version="5.0.1.1416"/>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="9.0.6" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="9.0.6" />
|
||||
|
||||
+1
-1
@@ -127,6 +127,7 @@ public static class SimApiExtensions
|
||||
x.OperationFilter<SimApiResponseOperationFilter>();
|
||||
x.OperationFilter<SimApiSignOperationFilter>();
|
||||
x.OperationFilter<AesBodyOperationFilter>();
|
||||
x.SchemaFilter<GlobalDynamicObjectSchemaFilter>();
|
||||
if (simApiOptions.EnableSimApiAuth)
|
||||
{
|
||||
x.OperationFilter<SimApiAuthOperationFilter>();
|
||||
@@ -239,7 +240,6 @@ public static class SimApiExtensions
|
||||
if (simApiOptions.EnableSimApiResponseFilter)
|
||||
{
|
||||
builder.AddControllers(opt => opt.Filters.Add<SimApiResponseFilter>())
|
||||
.AddXmlSerializerFormatters()
|
||||
.AddJsonOptions(opt =>
|
||||
{
|
||||
opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Microsoft.OpenApi.Any;
|
||||
|
||||
namespace SimApi.SwaggerFilters;
|
||||
|
||||
public class GlobalDynamicObjectSchemaFilter : ISchemaFilter
|
||||
{
|
||||
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
|
||||
{
|
||||
if (!IsDynamicObjectType(context.Type)) return;
|
||||
schema.AdditionalPropertiesAllowed = true;
|
||||
schema.AdditionalProperties = new OpenApiSchema
|
||||
{
|
||||
Type = "object", // 表示 value 可以是任意类型(兼容所有类型)
|
||||
Nullable = true
|
||||
};
|
||||
|
||||
// 2. 覆盖默认示例,使用包含多种类型的示例
|
||||
schema.Example = CreateMultiTypeExample();
|
||||
}
|
||||
|
||||
// 判断是否为需要处理的“动态对象”类型
|
||||
private bool IsDynamicObjectType(Type? type)
|
||||
{
|
||||
if (type == null) return false;
|
||||
if (typeof(IDictionary).IsAssignableFrom(type) ||
|
||||
(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (type == typeof(object))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return type.Name.Contains("AnonymousType") && type.Namespace == null;
|
||||
}
|
||||
|
||||
// 创建包含多种类型的示例(覆盖默认的 string 示例)
|
||||
private OpenApiObject CreateMultiTypeExample()
|
||||
{
|
||||
return new OpenApiObject
|
||||
{
|
||||
["stringProp"] = new OpenApiString("example string"), // 字符串
|
||||
["numberProp"] = new OpenApiInteger(123), // 数字
|
||||
["boolProp"] = new OpenApiBoolean(true), // 布尔值
|
||||
["objectProp"] = new OpenApiObject // 嵌套对象
|
||||
{
|
||||
["nestedKey"] = new OpenApiString("nested value")
|
||||
},
|
||||
["arrayProp"] = new OpenApiArray // 数组
|
||||
{
|
||||
new OpenApiInteger(1),
|
||||
new OpenApiString("two")
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user