1- using System ;
1+ using System ;
22using Swashbuckle . AspNetCore . Annotations ;
33
44namespace SimApi . Attributes ;
55
66/// <summary>
7- /// 快捷自定义接口文档类
7+ /// 快捷自定义接口文档类(所有参数均可省略, 支持命名参数)。
8+ /// 对应仓颉版 SimApiDoc:
9+ /// tags → 接口标签(逗号分隔, 如 "认证,用户")
10+ /// name → API 名称(映射到 Summary, 作为文档接口标题)
11+ /// description → API 详细描述
12+ /// groupNames → 所属文档组(逗号分隔, 如 "api,admin"; "*" 表示所有文档; null/空 → 仅默认 "api" 文档)
13+ /// ignore → true 时不出现在任何文档中(路由不受影响)
14+ /// 写法示例:
15+ /// [SimApiDoc("认证", "登录")] 位置参数(保持旧版兼容)
16+ /// [SimApiDoc(tags: "认证", name: "登录", description: "...")]
17+ /// [SimApiDoc(groupNames: "api,admin")] 仅指定文档组
18+ /// [SimApiDoc(GroupNames = "*", Ignore = false)] 属性命名参数
19+ /// [SimApiDoc] 全部默认(可标在类上, 仅用 Ignore/GroupNames 等)
20+ /// 属性命名参数优先于构造命名参数。
821/// </summary>
922[ AttributeUsage ( AttributeTargets . Method | AttributeTargets . Class ) ]
1023public class SimApiDocAttribute : SwaggerOperationAttribute
1124{
1225 /// <summary>
13- /// 定义接口说明
26+ /// 出现在所有文档组中的通配符
1427 /// </summary>
15- /// <param name="tags">接口分组列表</param>
28+ public const string AllGroups = "*" ;
29+
30+ /// <summary>
31+ /// 接口所属的文档组(逗号分隔, 如 "api,admin"); "*" 表示所有文档; null/空 表示未分组(仅默认 "api" 文档)
32+ /// </summary>
33+ public string ? GroupNames { get ; set ; }
34+
35+ /// <summary>
36+ /// 为 true 时该接口不出现在任何文档中(不影响路由)
37+ /// </summary>
38+ public bool Ignore { get ; set ; }
39+
40+ /// <summary>
41+ /// 定义接口说明(全部可选)
42+ /// </summary>
43+ /// <param name="tags">接口标签, 逗号分隔, 如 "认证,用户"</param>
1644 /// <param name="name">接口名称</param>
1745 /// <param name="description">接口描述</param>
18- public SimApiDocAttribute ( string [ ] tags , string name , string ? description = null )
46+ /// <param name="groupNames">所属文档组, 逗号分隔; "*" 表示所有文档</param>
47+ /// <param name="ignore">true 时不出现在任何文档</param>
48+ public SimApiDocAttribute ( string ? tags = null , string ? name = null , string ? description = null ,
49+ string ? groupNames = null , bool ignore = false )
1950 {
20- Tags = tags ;
21- Summary = name ;
22- if ( description != null )
23- {
24- Description = description ;
25- }
26- // Consumes = new[] {"application/json"};
27- // Produces = new[] {"application/json"};
51+ Apply ( tags , name , description , groupNames , ignore ) ;
2852 }
2953
3054 /// <summary>
31- /// 定义接口说明
55+ /// 定义接口说明(标签以数组传入)
3256 /// </summary>
33- /// <param name="tag">接口分组 </param>
57+ /// <param name="tags">接口标签列表 </param>
3458 /// <param name="name">接口名称</param>
3559 /// <param name="description">接口描述</param>
36- public SimApiDocAttribute ( string tag , string name , string ? description = null ) : this ( [ tag ] , name , description )
60+ /// <param name="groupNames">所属文档组, 逗号分隔; "*" 表示所有文档</param>
61+ /// <param name="ignore">true 时不出现在任何文档</param>
62+ public SimApiDocAttribute ( string [ ] tags , string ? name = null , string ? description = null ,
63+ string ? groupNames = null , bool ignore = false )
64+ {
65+ Apply ( tags , name , description , groupNames , ignore ) ;
66+ }
67+
68+ private void Apply ( string ? tags , string ? name , string ? description ,
69+ string ? groupNames , bool ignore )
70+ {
71+ if ( ! string . IsNullOrWhiteSpace ( tags ) )
72+ {
73+ Tags = tags . Split ( ',' , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) ;
74+ }
75+
76+ ApplyCore ( name , description , groupNames , ignore ) ;
77+ }
78+
79+ private void Apply ( string [ ] tags , string ? name , string ? description ,
80+ string ? groupNames , bool ignore )
3781 {
82+ if ( tags is { Length : > 0 } )
83+ {
84+ Tags = tags ;
85+ }
86+
87+ ApplyCore ( name , description , groupNames , ignore ) ;
88+ }
89+
90+ private void ApplyCore ( string ? name , string ? description , string ? groupNames , bool ignore )
91+ {
92+ if ( ! string . IsNullOrEmpty ( name ) )
93+ {
94+ Summary = name ;
95+ }
96+
97+ if ( ! string . IsNullOrEmpty ( description ) )
98+ {
99+ Description = description ;
100+ }
101+
102+ GroupNames = groupNames ;
103+ Ignore = ignore ;
38104 }
39- }
105+ }
0 commit comments