Compare commits

...
4 Commits
Author SHA1 Message Date
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
xrain c448381f23 fix middleware bug 2025-11-03 21:41:35 +08:00
xrain 8d5b667c88 fix 2025-11-02 22:18:25 +08:00
7 changed files with 96 additions and 14 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ namespace SimApi.Communications;
/// </summary>
public class SimApiLoginItem
{
public string Id { get; set; } = null!;
public required string Id { get; set; }
public string[] Type { get; set; } = ["user"];
public Dictionary<string, string> Meta { get; set; } = [];
public object? Extra { get; set; }
+17
View File
@@ -4,13 +4,30 @@ public class SimApiJobOptions
{
/// <summary>
/// WebUi地址,设置为null表示不启用
/// 默认 /jobs
/// </summary>
public string? DashboardUrl { get; set; } = "/jobs";
/// <summary>
/// webui 用户
/// 默认 admin
/// </summary>
public string DashboardAuthUser { get; set; } = "admin";
/// <summary>
/// webui 密码
/// 默认 Admin@123!
/// </summary>
public string DashboardAuthPass { get; set; } = "Admin@123!";
/// <summary>
/// 设置为null 使用默认redis配置
/// </summary>
public string? RedisConfiguration { get; set; }
/// <summary>
/// 设置为null 使用默认redis配置
/// </summary>
public int? Database { get; set; } = null;
public SimApiJobServerConfig[] Servers { get; set; } = [new()];
}
+1 -1
View File
@@ -11,7 +11,7 @@ public class SimApiJobWebAuth(string user, string pass) : IDashboardAuthorizatio
public bool Authorize(DashboardContext context)
{
var httpContext = context.GetHttpContext();
var authHeader = httpContext.Request.Headers["Authorization"].FirstOrDefault();
var authHeader = httpContext.Request.Headers.Authorization.FirstOrDefault();
if (authHeader != null && authHeader.StartsWith("Basic "))
{
var encodedUsernamePassword = authHeader.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries)[1].Trim();
+3
View File
@@ -24,6 +24,8 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
try
{
await next(context);
if (!context.Response.HasStarted)
{
switch (context.Response.StatusCode)
{
case 200:
@@ -36,6 +38,7 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
throw new SimApiException(context.Response.StatusCode);
}
}
}
catch (SimApiException ex)
{
response = string.IsNullOrEmpty(ex.Message)
+2 -2
View File
@@ -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
View File
@@ -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")
}
};
}
}