From 81e166ad45a43bca2f12d22670d10a6fd2feab6b Mon Sep 17 00:00:00 2001 From: xRain Date: Tue, 23 Dec 2025 08:18:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86swgger=E7=94=9F?= =?UTF-8?q?=E6=88=90=E7=9A=84=E7=B1=BB=E5=9E=8B=E5=90=8D,SimApiAuth=20?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E4=B8=8D=E5=8A=A0=E5=8F=82=E6=95=B0,?= =?UTF-8?q?=E5=88=99=E9=BB=98=E8=AE=A4=E6=8E=A5=E5=8F=97=E6=89=80=E6=9C=89?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E4=BA=86=E7=9A=84=E7=94=A8=E6=88=B7=E8=AF=B7?= =?UTF-8?q?=E6=B1=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Attributes/SimApiAuthAttribute.cs | 13 ++----- SimApiExtensions.cs | 61 ++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 23 deletions(-) diff --git a/Attributes/SimApiAuthAttribute.cs b/Attributes/SimApiAuthAttribute.cs index db0a229..2c66aaf 100644 --- a/Attributes/SimApiAuthAttribute.cs +++ b/Attributes/SimApiAuthAttribute.cs @@ -13,13 +13,13 @@ namespace SimApi.Attributes; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public class SimApiAuthAttribute : ActionFilterAttribute { - private string[] Types { get; } + private string[]? Types { get; } //默认是user登录类型 public SimApiAuthAttribute() { - Types = new[] { "user" }; + Types = null; } //只检测一种用户类型的快捷方式 @@ -34,13 +34,6 @@ public class SimApiAuthAttribute : ActionFilterAttribute Types = types; } - //只检测一种用户类型的快捷方式 - public SimApiAuthAttribute(string type, string url) - { - Types = new[] { type }; - new HttpPostAttribute(url); - } - public override void OnActionExecuting(ActionExecutingContext context) { var loginInfo = (SimApiLoginItem)context.HttpContext.Items["LoginInfo"]!; @@ -49,6 +42,8 @@ public class SimApiAuthAttribute : ActionFilterAttribute { throw new SimApiException(401); } + + if (Types == null) return; //检测用户类型 if (!Types.Intersect(loginInfo.Type).Any()) { diff --git a/SimApiExtensions.cs b/SimApiExtensions.cs index d9c1235..6457889 100644 --- a/SimApiExtensions.cs +++ b/SimApiExtensions.cs @@ -126,14 +126,38 @@ public static class SimApiExtensions x.CustomSchemaIds(type => { - string GetSimpleTypeName(Type t) + // 递归解析类型名称(处理嵌套泛型/数组/可空 + 保证唯一性) + string GetSimpleTypeName(Type t, int depth = 0) { + // 防止无限递归 + if (depth > 5) return t.Name.Split('`')[0]; + + // 处理数组类型 if (t.IsArray) { var elementType = t.GetElementType(); - return $"{GetSimpleTypeName(elementType)}[]"; + return $"{GetSimpleTypeName(elementType, depth + 1)}[]"; } + // 处理可空类型 + if (Nullable.GetUnderlyingType(t) != null) + { + var underlyingType = Nullable.GetUnderlyingType(t); + return GetSimpleTypeName(underlyingType, depth + 1); + } + + // 处理泛型类型(递归解析嵌套泛型) + if (t.IsGenericType) + { + var genericBaseName = t.GetGenericTypeDefinition().Name.Split('`')[0]; + var genericArgs = t.GetGenericArguments() + .Select(arg => GetSimpleTypeName(arg, depth + 1)) + .Where(arg => !string.IsNullOrEmpty(arg)) + .ToArray(); + return $"{genericBaseName}<{string.Join(",", genericArgs)}>"; + } + + // 处理基础类型(小写) if (t.IsPrimitive || t == typeof(string) || t == typeof(DateTime) || t == typeof(Guid)) { return t.Name switch @@ -142,23 +166,32 @@ public static class SimApiExtensions "Int32" => "int", "Int64" => "long", "Boolean" => "boolean", - "DateTime" => "DateTime", - "Guid" => "Guid", - _ => t.Name + "DateTime" => "datetime", + "Guid" => "guid", + _ => t.Name.ToLower() }; } - return t.Name; + // 核心修复:生成唯一名称(处理嵌套类/同名不同类) + var typeName = t.Name; + + // 步骤1:处理嵌套类(如 ApplicationDto+ApplicationEditRequest → ApplicationDto_ApplicationEditRequest) + if (t.DeclaringType != null) + { + typeName = $"{GetSimpleTypeName(t.DeclaringType)}.{typeName}"; + } + + // 步骤2:(可选)处理同命名空间下的同名类(拼接命名空间前缀,避免全局重复) + // 如需更严格的唯一性,取消注释下面这行 + // typeName = $"{t.Namespace?.Replace(".", "_")}_{typeName}"; + + return typeName; } - if (!type.IsGenericType) return GetSimpleTypeName(type); - var baseName = type.GetGenericTypeDefinition().Name.Split('`')[0]; - var genericArgs = type.GetGenericArguments() - .Select(GetSimpleTypeName) - .ToArray(); - return genericArgs.Length > 0 - ? $"{baseName}<{string.Join(",", genericArgs)}>" - : baseName; + // 根调用:解析当前类型 + var uniqueSchemaId = GetSimpleTypeName(type); + // 可选:移除特殊字符(如 $、+),避免 Swagger 解析问题 + return uniqueSchemaId.Replace("$", "").Replace("+", "_"); }); x.OperationFilter(); x.OperationFilter();