Compare commits

...
3 Commits
Author SHA1 Message Date
xrain 90124b6e67 update v10 2025-12-18 09:08:35 +08:00
xrain 2499912204 update package 2025-11-09 19:41:37 +08:00
xrain dd38f315d5 remove xml , support object to any in openapi 2025-11-09 19:36:21 +08:00
7 changed files with 108 additions and 37 deletions
+6 -6
View File
@@ -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>
@@ -17,14 +17,14 @@
<Folder Include="Exceptions\"/> <Folder Include="Exceptions\"/>
</ItemGroup> </ItemGroup>
<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.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="6.0.5" /> <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.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="9.0.6" /> <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.0.1" />
</ItemGroup> </ItemGroup>
<ProjectExtensions> <ProjectExtensions>
<MonoDevelop> <MonoDevelop>
+2 -2
View File
@@ -9,7 +9,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;
@@ -127,6 +127,7 @@ public static class SimApiExtensions
x.OperationFilter<SimApiResponseOperationFilter>(); x.OperationFilter<SimApiResponseOperationFilter>();
x.OperationFilter<SimApiSignOperationFilter>(); x.OperationFilter<SimApiSignOperationFilter>();
x.OperationFilter<AesBodyOperationFilter>(); x.OperationFilter<AesBodyOperationFilter>();
x.SchemaFilter<GlobalDynamicObjectSchemaFilter>();
if (simApiOptions.EnableSimApiAuth) if (simApiOptions.EnableSimApiAuth)
{ {
x.OperationFilter<SimApiAuthOperationFilter>(); x.OperationFilter<SimApiAuthOperationFilter>();
@@ -239,7 +240,6 @@ public static class SimApiExtensions
if (simApiOptions.EnableSimApiResponseFilter) if (simApiOptions.EnableSimApiResponseFilter)
{ {
builder.AddControllers(opt => opt.Filters.Add<SimApiResponseFilter>()) builder.AddControllers(opt => opt.Filters.Add<SimApiResponseFilter>())
.AddXmlSerializerFormatters()
.AddJsonOptions(opt => .AddJsonOptions(opt =>
{ {
opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
+2 -1
View File
@@ -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;
@@ -0,0 +1,70 @@
using System;
using Microsoft.OpenApi;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections;
using System.Collections.Generic;
using System.Text.Json.Nodes;
namespace SimApi.SwaggerFilters;
public class GlobalDynamicObjectSchemaFilter : ISchemaFilter
{
public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
{
if (!IsDynamicObjectType(context.Type)) return;
var oaSchema = schema as OpenApiSchema;
oaSchema.AdditionalPropertiesAllowed = true;
oaSchema.AdditionalProperties = new OpenApiSchema
{
Type = JsonSchemaType.Object, // 表示 value 可以是任意类型(兼容所有类型)
};
// 2. 覆盖默认示例,使用包含多种类型的示例
oaSchema.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 JsonNode CreateMultiTypeExample()
{
return new JsonObject
{
// 字符串类型:JsonValue.Create 包装字符串
["stringProp"] = JsonValue.Create("example string"),
// 数字类型:支持 int/long/double 等,JsonValue 自动适配
["numberProp"] = JsonValue.Create(123),
// 布尔类型
["boolProp"] = JsonValue.Create(true),
// 嵌套对象:JsonObject 对应 OpenApiObject
["objectProp"] = new JsonObject
{
["nestedKey"] = JsonValue.Create("nested value")
},
// 数组类型:JsonArray 对应 OpenApiArray
["arrayProp"] = new JsonArray
{
JsonValue.Create(1), // 数组内数字
JsonValue.Create("two") // 数组内字符串
},
// 可选:添加 null 值示例(若需要)
["nullProp"] = null
};
}
}
+17 -21
View File
@@ -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,24 @@ 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;
// 关键修复:正确构造 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
{ {
// 添加授权要求:关联步骤 2 中定义的 "SimApiAuth" 安全方案
operation.Security = new List<OpenApiSecurityRequirement>
{ {
new OpenApiSecurityRequirement securitySchemeRef,
{ []
{ }
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;
+10 -6
View File
@@ -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 // 签名相关参数通常为字符串类型
} }
}); });
} }