Compare commits

...
3 Commits
Author SHA1 Message Date
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
6 changed files with 94 additions and 12 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();
+12 -9
View File
@@ -24,16 +24,19 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
try
{
await next(context);
switch (context.Response.StatusCode)
if (!context.Response.HasStarted)
{
case 200:
case 301:
case 302:
break;
case 404:
throw new SimApiException(context.Response.StatusCode, "请求的接口不存在");
default:
throw new SimApiException(context.Response.StatusCode);
switch (context.Response.StatusCode)
{
case 200:
case 301:
case 302:
break;
case 404:
throw new SimApiException(context.Response.StatusCode, "请求的接口不存在");
default:
throw new SimApiException(context.Response.StatusCode);
}
}
}
catch (SimApiException ex)
+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")
}
};
}
}