-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetProSwaggerServiceExtensions.cs
More file actions
206 lines (185 loc) · 8.67 KB
/
NetProSwaggerServiceExtensions.cs
File metadata and controls
206 lines (185 loc) · 8.67 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.OpenApi.Models;
using NetPro.TypeFinder;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace NetPro.Swagger
{
public static class NetProSwaggerServiceExtensions
{
public static IServiceCollection AddNetProSwagger(this IServiceCollection services, IConfiguration configuration)
{
services.AddFileProcessService();
var loggerFactory = services.BuildServiceProvider().GetService<ILoggerFactory>();
ILogger logger = null;
if (loggerFactory != null)
{
logger = loggerFactory.CreateLogger($"{nameof(NetProSwaggerServiceExtensions)}");
}
if (!configuration.GetValue<bool>("SwaggerOption:Enabled", false))
{
logger?.LogInformation($"NetPro Swagger 已关闭");
return services;
}
else
logger?.LogInformation($"NetPro Swagger 已启用");
services
.Configure<OpenApiInfo>(configuration.GetSection("SwaggerOption"));
var info = services.BuildServiceProvider().GetService<IOptions<OpenApiInfo>>().Value;
services.AddSwaggerGen(c =>
{
c.DescribeAllParametersInCamelCase();//请求参数转小写
c.OperationFilter<SwaggerFileUploadFilter>();//add file fifter component
c.OperationFilter<SwaggerDefaultValueFilter>();//add webapi default value of parameter
c.OperationFilter<CustomerHeaderParameter>();//add default header
c.OperationFilter<CustomerQueryParameter>();//add default query
var securityRequirement = new OpenApiSecurityRequirement();
securityRequirement.Add(new OpenApiSecurityScheme { Name = "Bearer" }, new string[] { });
c.AddSecurityRequirement(securityRequirement);
//batch find xml file of swagger
var basePath = PlatformServices.Default.Application.ApplicationBasePath;//get app root path
List<string> xmlComments = GetXmlComments();
xmlComments.ForEach(r =>
{
string filePath = Path.Combine(basePath, r);
if (File.Exists(filePath))
{
c.IncludeXmlComments(filePath);
}
});
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = info.Title,
Version = info.Version,
Description = info.Description,
//TermsOfService = "None",
Contact = info.Contact,// new OpenApiContact { Email = "Email", Name = "Name", Url = new Uri("http://www.github.com") },
License = info.License,//new OpenApiLicense { Url = new Uri("http://www.github.com"), Name = "LicenseName" },
});
c.IgnoreObsoleteActions();
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "Authority certification(The data is transferred in the request header) structure of the parameters : \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}
});
});
services.AddSwaggerGenNewtonsoftSupport();
return services;
}
/// <summary>
/// 所有xml默认当作swagger文档注入swagger
/// </summary>
/// <returns></returns>
private static List<string> GetXmlComments()
{
//var pattern = $"^{netProOption.ProjectPrefix}.*({netProOption.ProjectSuffix}|Domain)$";
//List<string> assemblyNames = ReflectionHelper.GetAssemblyNames(pattern);
List<string> assemblyNames = AppDomain.CurrentDomain.GetAssemblies().Select(s => s.GetName().Name).ToList();
List<string> xmlFiles = new List<string>();
assemblyNames.ForEach(r =>
{
string fileName = $"{r}.xml";
xmlFiles.Add(fileName);
});
return xmlFiles;
}
}
public static class NetProSwaggerMiddlewareExtensions
{
public static IApplicationBuilder UseNetProSwagger(
this IApplicationBuilder application)
{
var configuration = application.ApplicationServices.GetService(typeof(IConfiguration)) as IConfiguration;
if (configuration.GetValue<bool>("SwaggerOption:Enabled", false))
{
var openApiInfo = configuration.GetSection("SwaggerOption").Get<OpenApiInfo>();
application.UseSwagger(c =>
{
c.RouteTemplate = "docs/{documentName}/docs.json";//使中间件服务生成Swagger作为JSON端点
c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Info.Description = httpReq.Path);//请求过滤处理
});
//application.UseReDoc(c =>
//{
//c.SpecUrl("/docs/v1/docs.json");//此处配置要和UseSwagger的RouteTemplate匹配
////c.EnableUntrustedSpec();
//c.ScrollYOffset(10);
////c.HideHostname();
////c.HideDownloadButton();
//c.RequiredPropsFirst();
////c.NoAutoAuth();
//c.PathInMiddlePanel();
////c.HideLoading();
//c.NativeScrollbars();
////c.DisableSearch();
////c.OnlyRequiredInSamples();
////c.SortPropsAlphabetically();
//c.RoutePrefix = $"{configuration.GetValue<string>("SwaggerOption:RoutePrefix", "swagger")}";//设置文档首页根路径
// });
application.UseSwaggerUI(c =>
{
c.DocExpansion(DocExpansion.List);
c.EnableDeepLinking();
c.EnableFilter();
c.MaxDisplayedTags(5);
c.ShowExtensions();
c.ShowCommonExtensions();
c.EnableValidator();
//c.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Head);
c.RoutePrefix = $"{configuration.GetValue<string>("SwaggerOption:RoutePrefix", "swagger")}";//设置文档首页根路径
c.SwaggerEndpoint("/docs/v1/docs.json", $"{openApiInfo.Title}");//此处配置要和UseSwagger的RouteTemplate匹配
c.SwaggerEndpoint("https://petstore.swagger.io/v2/swagger.json", "petstore.swagger");//远程swagger示例
#region
typeof(NetProSwaggerMiddlewareExtensions).GetTypeInfo().Assembly.GetManifestResourceStream("NetPro.Swagger.SwaggerProfiler.html");
#endregion
if (configuration.GetValue<bool>("SwaggerOption:IsDarkTheme", false))
c.IndexStream = () => typeof(NetProSwaggerMiddlewareExtensions).GetTypeInfo().Assembly.GetManifestResourceStream("NetPro.Swagger.IndexDark.html");
});
}
return application;
}
}
public class NetProSwaggerMiddleware
{
private readonly RequestDelegate _next;
public NetProSwaggerMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
await _next(context);
return;
}
}
}