SpecificationDocumentSettingsOptions.cs
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using NCC.ConfigurableOptions;
using NCC.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Linq;
namespace NCC.SpecificationDocument
{
/// <summary>
/// 规范化文档配置选项
/// </summary>
public sealed class SpecificationDocumentSettingsOptions : IConfigurableOptions<SpecificationDocumentSettingsOptions>
{
/// <summary>
/// 文档标题
/// </summary>
public string DocumentTitle { get; set; }
/// <summary>
/// 默认分组名
/// </summary>
public string DefaultGroupName { get; set; }
/// <summary>
/// 启用授权支持
/// </summary>
public bool? EnableAuthorized { get; set; }
/// <summary>
/// 格式化为V2版本
/// </summary>
public bool? FormatAsV2 { get; set; }
/// <summary>
/// 配置规范化文档地址
/// </summary>
public string RoutePrefix { get; set; }
/// <summary>
/// 文档展开设置
/// </summary>
public DocExpansion? DocExpansionState { get; set; }
/// <summary>
/// XML 描述文件
/// </summary>
public string[] XmlComments { get; set; }
/// <summary>
/// 分组信息
/// </summary>
public SpecificationOpenApiInfo[] GroupOpenApiInfos { get; set; }
/// <summary>
/// 安全定义
/// </summary>
public SpecificationOpenApiSecurityScheme[] SecurityDefinitions { get; set; }
/// <summary>
/// 配置 Servers
/// </summary>
public OpenApiServer[] Servers { get; set; }
/// <summary>
/// 隐藏 Servers
/// </summary>
public bool? HideServers { get; set; }
/// <summary>
/// 默认 swagger.json 路由模板
/// </summary>
public string RouteTemplate { get; set; }
/// <summary>
/// 配置安装第三方包的分组名
/// </summary>
public string[] PackagesGroups { get; set; }
/// <summary>
/// 启用枚举 Schema 筛选器
/// </summary>
public bool? EnableEnumSchemaFilter { get; set; }
/// <summary>
/// 启用标签排序筛选器
/// </summary>
public bool? EnableTagsOrderDocumentFilter { get; set; }
/// <summary>
/// 后期配置
/// </summary>
/// <param name="options"></param>
/// <param name="configuration"></param>
public void PostConfigure(SpecificationDocumentSettingsOptions options, IConfiguration configuration)
{
options.DocumentTitle ??= "Specification Api Document";
options.DefaultGroupName ??= "Default";
options.FormatAsV2 ??= false;
//options.RoutePrefix ??= "api"; // 可以通过 UseInject() 配置,所以注释
options.DocExpansionState ??= DocExpansion.List;
// 加载项目注册和模块化/插件注释
var frameworkPackageName = Reflect.GetAssemblyName(GetType());
var projectXmlComments = App.Assemblies.Where(u => u.GetName().Name != frameworkPackageName).Select(t => t.GetName().Name);
var externalXmlComments = App.ExternalAssemblies.Any() ? App.Settings.ExternalAssemblies.Select(u => u.EndsWith(".dll") ? u[0..^4] : u) : Array.Empty<string>();
XmlComments ??= projectXmlComments.Concat(externalXmlComments).ToArray();
GroupOpenApiInfos ??= new SpecificationOpenApiInfo[]
{
new SpecificationOpenApiInfo()
{
Group=options.DefaultGroupName
}
};
EnableAuthorized ??= true;
if (EnableAuthorized == true)
{
SecurityDefinitions ??= new SpecificationOpenApiSecurityScheme[]
{
new SpecificationOpenApiSecurityScheme
{
Id="Bearer",
Type= SecuritySchemeType.Http,
Name="Authorization",
Description="JWT Authorization header using the Bearer scheme.",
BearerFormat="JWT",
Scheme="bearer",
In= ParameterLocation.Header,
Requirement=new SpecificationOpenApiSecurityRequirementItem
{
Scheme=new OpenApiSecurityScheme
{
Reference=new OpenApiReference
{
Id="Bearer",
Type= ReferenceType.SecurityScheme
}
},
Accesses=Array.Empty<string>()
}
}
};
}
Servers ??= Array.Empty<OpenApiServer>();
HideServers ??= true;
RouteTemplate ??= "swagger/{documentName}/swagger.json";
PackagesGroups ??= Array.Empty<string>();
EnableEnumSchemaFilter ??= true;
EnableTagsOrderDocumentFilter ??= true;
}
}
}