Skip to content

Commit 81e166a

Browse files
committed
修正了swgger生成的类型名,SimApiAuth 如果不加参数,则默认接受所有登录了的用户请求.
1 parent d50d32f commit 81e166a

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

Attributes/SimApiAuthAttribute.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ namespace SimApi.Attributes;
1313
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
1414
public class SimApiAuthAttribute : ActionFilterAttribute
1515
{
16-
private string[] Types { get; }
16+
private string[]? Types { get; }
1717

1818

1919
//默认是user登录类型
2020
public SimApiAuthAttribute()
2121
{
22-
Types = new[] { "user" };
22+
Types = null;
2323
}
2424

2525
//只检测一种用户类型的快捷方式
@@ -34,13 +34,6 @@ public SimApiAuthAttribute(string[] types)
3434
Types = types;
3535
}
3636

37-
//只检测一种用户类型的快捷方式
38-
public SimApiAuthAttribute(string type, string url)
39-
{
40-
Types = new[] { type };
41-
new HttpPostAttribute(url);
42-
}
43-
4437
public override void OnActionExecuting(ActionExecutingContext context)
4538
{
4639
var loginInfo = (SimApiLoginItem)context.HttpContext.Items["LoginInfo"]!;
@@ -49,6 +42,8 @@ public override void OnActionExecuting(ActionExecutingContext context)
4942
{
5043
throw new SimApiException(401);
5144
}
45+
46+
if (Types == null) return;
5247
//检测用户类型
5348
if (!Types.Intersect(loginInfo.Type).Any())
5449
{

SimApiExtensions.cs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,38 @@ public static IServiceCollection AddSimApi(this IServiceCollection builder,
126126

127127
x.CustomSchemaIds(type =>
128128
{
129-
string GetSimpleTypeName(Type t)
129+
// 递归解析类型名称(处理嵌套泛型/数组/可空 + 保证唯一性)
130+
string GetSimpleTypeName(Type t, int depth = 0)
130131
{
132+
// 防止无限递归
133+
if (depth > 5) return t.Name.Split('`')[0];
134+
135+
// 处理数组类型
131136
if (t.IsArray)
132137
{
133138
var elementType = t.GetElementType();
134-
return $"{GetSimpleTypeName(elementType)}[]";
139+
return $"{GetSimpleTypeName(elementType, depth + 1)}[]";
140+
}
141+
142+
// 处理可空类型
143+
if (Nullable.GetUnderlyingType(t) != null)
144+
{
145+
var underlyingType = Nullable.GetUnderlyingType(t);
146+
return GetSimpleTypeName(underlyingType, depth + 1);
147+
}
148+
149+
// 处理泛型类型(递归解析嵌套泛型)
150+
if (t.IsGenericType)
151+
{
152+
var genericBaseName = t.GetGenericTypeDefinition().Name.Split('`')[0];
153+
var genericArgs = t.GetGenericArguments()
154+
.Select(arg => GetSimpleTypeName(arg, depth + 1))
155+
.Where(arg => !string.IsNullOrEmpty(arg))
156+
.ToArray();
157+
return $"{genericBaseName}<{string.Join(",", genericArgs)}>";
135158
}
136159

160+
// 处理基础类型(小写)
137161
if (t.IsPrimitive || t == typeof(string) || t == typeof(DateTime) || t == typeof(Guid))
138162
{
139163
return t.Name switch
@@ -142,23 +166,32 @@ string GetSimpleTypeName(Type t)
142166
"Int32" => "int",
143167
"Int64" => "long",
144168
"Boolean" => "boolean",
145-
"DateTime" => "DateTime",
146-
"Guid" => "Guid",
147-
_ => t.Name
169+
"DateTime" => "datetime",
170+
"Guid" => "guid",
171+
_ => t.Name.ToLower()
148172
};
149173
}
150174

151-
return t.Name;
175+
// 核心修复:生成唯一名称(处理嵌套类/同名不同类)
176+
var typeName = t.Name;
177+
178+
// 步骤1:处理嵌套类(如 ApplicationDto+ApplicationEditRequest → ApplicationDto_ApplicationEditRequest)
179+
if (t.DeclaringType != null)
180+
{
181+
typeName = $"{GetSimpleTypeName(t.DeclaringType)}.{typeName}";
182+
}
183+
184+
// 步骤2:(可选)处理同命名空间下的同名类(拼接命名空间前缀,避免全局重复)
185+
// 如需更严格的唯一性,取消注释下面这行
186+
// typeName = $"{t.Namespace?.Replace(".", "_")}_{typeName}";
187+
188+
return typeName;
152189
}
153190

154-
if (!type.IsGenericType) return GetSimpleTypeName(type);
155-
var baseName = type.GetGenericTypeDefinition().Name.Split('`')[0];
156-
var genericArgs = type.GetGenericArguments()
157-
.Select(GetSimpleTypeName)
158-
.ToArray();
159-
return genericArgs.Length > 0
160-
? $"{baseName}<{string.Join(",", genericArgs)}>"
161-
: baseName;
191+
// 根调用:解析当前类型
192+
var uniqueSchemaId = GetSimpleTypeName(type);
193+
// 可选:移除特殊字符(如 $、+),避免 Swagger 解析问题
194+
return uniqueSchemaId.Replace("$", "").Replace("+", "_");
162195
});
163196
x.OperationFilter<SimApiResponseOperationFilter>();
164197
x.OperationFilter<SimApiSignOperationFilter>();

0 commit comments

Comments
 (0)