-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AspNetCoreToSwaggerGenerator.cs
277 lines (229 loc) · 13.5 KB
/
AspNetCoreToSwaggerGenerator.cs
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//-----------------------------------------------------------------------
// <copyright file="AspNetCoreToSwaggerGenerator.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NJsonSchema;
using NJsonSchema.Infrastructure;
using NSwag.SwaggerGeneration.Processors;
using NSwag.SwaggerGeneration.Processors.Contexts;
namespace NSwag.SwaggerGeneration.AspNetCore
{
/// <summary>Generates a <see cref="SwaggerDocument"/> using <see cref="ApiDescription"/>. </summary>
public class AspNetCoreToSwaggerGenerator : ISwaggerGenerator
{
private readonly SwaggerJsonSchemaGenerator _schemaGenerator;
/// <summary>Initializes a new instance of the <see cref="AspNetCoreToSwaggerGenerator" /> class.</summary>
/// <param name="settings">The settings.</param>
public AspNetCoreToSwaggerGenerator(AspNetCoreToSwaggerGeneratorSettings settings)
: this(settings, new SwaggerJsonSchemaGenerator(settings))
{
}
/// <summary>Initializes a new instance of the <see cref="AspNetCoreToSwaggerGenerator" /> class.</summary>
/// <param name="settings">The settings.</param>
/// <param name="schemaGenerator">The schema generator.</param>
public AspNetCoreToSwaggerGenerator(AspNetCoreToSwaggerGeneratorSettings settings, SwaggerJsonSchemaGenerator schemaGenerator)
{
Settings = settings;
_schemaGenerator = schemaGenerator;
}
/// <summary>Gets the generator settings.</summary>
public AspNetCoreToSwaggerGeneratorSettings Settings { get; }
/// <summary>Generates a Swagger specification for the given <see cref="ApiDescriptionGroupCollection"/>.</summary>
/// <param name="apiDescriptionGroups"><see cref="ApiDescriptionGroup"/>.</param>
/// <returns>The <see cref="SwaggerDocument" />.</returns>
/// <exception cref="InvalidOperationException">The operation has more than one body parameter.</exception>
public async Task<SwaggerDocument> GenerateAsync(ApiDescriptionGroupCollection apiDescriptionGroups)
{
var apiDescriptions = apiDescriptionGroups.Items.SelectMany(g => g.Items);
var document = await CreateDocumentAsync().ConfigureAwait(false);
var schemaResolver = new SwaggerSchemaResolver(document, Settings);
var apiGroups = apiDescriptions.Where(apiDescription => apiDescription.ActionDescriptor is ControllerActionDescriptor)
.Select(apiDescription => new Tuple<ApiDescription, ControllerActionDescriptor>(apiDescription, (ControllerActionDescriptor)apiDescription.ActionDescriptor))
.GroupBy(item => item.Item2.ControllerTypeInfo.AsType())
.ToArray();
var usedControllerTypes = new List<Type>();
foreach (var controllerApiDescriptionGroup in apiGroups)
{
var generator = new SwaggerGenerator(_schemaGenerator, Settings, schemaResolver);
var isIncluded = await GenerateForControllerAsync(document, controllerApiDescriptionGroup.Key, controllerApiDescriptionGroup, generator, schemaResolver).ConfigureAwait(false);
if (isIncluded)
usedControllerTypes.Add(controllerApiDescriptionGroup.Key);
}
document.GenerateOperationIds();
var controllerTypes = apiGroups.Select(k => k.Key).ToArray();
foreach (var processor in Settings.DocumentProcessors)
await processor.ProcessAsync(new DocumentProcessorContext(document, controllerTypes, usedControllerTypes, schemaResolver, _schemaGenerator, Settings));
return document;
}
/// <summary>Generates the <see cref="SwaggerDocument"/> with services from the given service provider.</summary>
/// <param name="serviceProvider">The service provider.</param>
/// <returns>The document</returns>
public async Task<SwaggerDocument> GenerateAsync(object serviceProvider)
{
var typedServiceProvider = (IServiceProvider)serviceProvider;
var mvcOptions = typedServiceProvider.GetRequiredService<IOptions<MvcOptions>>();
var mvcJsonOptions = typedServiceProvider.GetRequiredService<IOptions<MvcJsonOptions>>();
var apiDescriptionGroupCollectionProvider = typedServiceProvider.GetRequiredService<IApiDescriptionGroupCollectionProvider>();
Settings.ApplySettings(mvcJsonOptions.Value.SerializerSettings, mvcOptions.Value);
return await GenerateAsync(apiDescriptionGroupCollectionProvider.ApiDescriptionGroups);
}
private async Task<bool> GenerateForControllerAsync(SwaggerDocument document, Type controllerType,
IEnumerable<Tuple<ApiDescription, ControllerActionDescriptor>> controllerApiDescriptionGroup,
SwaggerGenerator swaggerGenerator, SwaggerSchemaResolver schemaResolver)
{
var hasIgnoreAttribute = controllerType.GetTypeInfo()
.GetCustomAttributes()
.Any(a => a.GetType().Name == "SwaggerIgnoreAttribute");
if (hasIgnoreAttribute)
{
return false;
}
var operations = new List<Tuple<SwaggerOperationDescription, ApiDescription, MethodInfo>>();
foreach (var item in controllerApiDescriptionGroup)
{
var apiDescription = item.Item1;
var method = item.Item2.MethodInfo;
var actionHasIgnoreAttribute = method.GetCustomAttributes().Any(a => a.GetType().Name == "SwaggerIgnoreAttribute");
if (actionHasIgnoreAttribute)
{
continue;
}
var path = apiDescription.RelativePath;
if (!path.StartsWith("/", StringComparison.Ordinal))
path = "/" + path;
var controllerActionDescriptor = (ControllerActionDescriptor)apiDescription.ActionDescriptor;
if (!Enum.TryParse<SwaggerOperationMethod>(apiDescription.HttpMethod, ignoreCase: true, result: out var swaggerOperationMethod))
swaggerOperationMethod = SwaggerOperationMethod.Undefined;
var operationDescription = new SwaggerOperationDescription
{
Path = path,
Method = swaggerOperationMethod,
Operation = new SwaggerOperation
{
IsDeprecated = method.GetCustomAttribute<ObsoleteAttribute>() != null,
OperationId = GetOperationId(document, controllerActionDescriptor, method)
}
};
operations.Add(new Tuple<SwaggerOperationDescription, ApiDescription, MethodInfo>(operationDescription, apiDescription, method));
}
return await AddOperationDescriptionsToDocumentAsync(document, controllerType, operations, swaggerGenerator, schemaResolver).ConfigureAwait(false);
}
private async Task<bool> AddOperationDescriptionsToDocumentAsync(SwaggerDocument document, Type controllerType, List<Tuple<SwaggerOperationDescription, ApiDescription, MethodInfo>> operations, SwaggerGenerator swaggerGenerator, SwaggerSchemaResolver schemaResolver)
{
var addedOperations = 0;
var allOperation = operations.Select(t => t.Item1).ToList();
foreach (var tuple in operations)
{
var operation = tuple.Item1;
var apiDescription = tuple.Item2;
var method = tuple.Item3;
for (var i = 0; i < apiDescription.SupportedRequestFormats.Count; i++)
{
var mediaType = apiDescription.SupportedRequestFormats[i].MediaType;
if (document.Consumes == null)
document.Consumes = new List<string>();
if (!document.Consumes.Contains(mediaType, StringComparer.OrdinalIgnoreCase))
{
document.Consumes.Add(mediaType);
}
}
var addOperation = await RunOperationProcessorsAsync(document, apiDescription, controllerType, method, operation, allOperation, swaggerGenerator, schemaResolver).ConfigureAwait(false);
if (addOperation)
{
var path = operation.Path.Replace("//", "/");
if (!document.Paths.ContainsKey(path))
document.Paths[path] = new SwaggerPathItem();
if (document.Paths[path].ContainsKey(operation.Method))
{
throw new InvalidOperationException($"The method '{operation.Method}' on path '{path}' is registered multiple times.");
}
document.Paths[path][operation.Method] = operation.Operation;
addedOperations++;
}
}
return addedOperations > 0;
}
private async Task<SwaggerDocument> CreateDocumentAsync()
{
var document = !string.IsNullOrEmpty(Settings.DocumentTemplate) ?
await SwaggerDocument.FromJsonAsync(Settings.DocumentTemplate).ConfigureAwait(false) :
new SwaggerDocument();
document.Generator = $"NSwag v{SwaggerDocument.ToolchainVersion} (NJsonSchema v{JsonSchema4.ToolchainVersion})";
document.SchemaType = Settings.SchemaType;
if (document.Info == null)
document.Info = new SwaggerInfo();
if (string.IsNullOrEmpty(Settings.DocumentTemplate))
{
if (!string.IsNullOrEmpty(Settings.Title))
document.Info.Title = Settings.Title;
if (!string.IsNullOrEmpty(Settings.Description))
document.Info.Description = Settings.Description;
if (!string.IsNullOrEmpty(Settings.Version))
document.Info.Version = Settings.Version;
}
return document;
}
private async Task<bool> RunOperationProcessorsAsync(SwaggerDocument document, ApiDescription apiDescription, Type controllerType, MethodInfo methodInfo, SwaggerOperationDescription operationDescription, List<SwaggerOperationDescription> allOperations, SwaggerGenerator swaggerGenerator, SwaggerSchemaResolver schemaResolver)
{
// 1. Run from settings
var operationProcessorContext = new AspNetCoreOperationProcessorContext(document, operationDescription, controllerType, methodInfo, swaggerGenerator, _schemaGenerator, schemaResolver, Settings, allOperations)
{
ApiDescription = apiDescription,
};
foreach (var operationProcessor in Settings.OperationProcessors)
{
if (await operationProcessor.ProcessAsync(operationProcessorContext).ConfigureAwait(false) == false)
return false;
}
// 2. Run from class attributes
var operationProcessorAttribute = methodInfo.DeclaringType.GetTypeInfo()
.GetCustomAttributes()
// 3. Run from method attributes
.Concat(methodInfo.GetCustomAttributes())
.Where(a => a.GetType().IsAssignableTo("SwaggerOperationProcessorAttribute", TypeNameStyle.Name));
foreach (dynamic attribute in operationProcessorAttribute)
{
var operationProcessor = ReflectionExtensions.HasProperty(attribute, "Parameters") ?
(IOperationProcessor)Activator.CreateInstance(attribute.Type, attribute.Parameters) :
(IOperationProcessor)Activator.CreateInstance(attribute.Type);
if (await operationProcessor.ProcessAsync(operationProcessorContext) == false)
return false;
}
return true;
}
private string GetOperationId(SwaggerDocument document, ControllerActionDescriptor actionDescriptor, MethodInfo method)
{
string operationId;
dynamic swaggerOperationAttribute = method.GetCustomAttributes().FirstOrDefault(a => a.GetType().Name == "SwaggerOperationAttribute");
if (swaggerOperationAttribute != null && !string.IsNullOrEmpty(swaggerOperationAttribute.OperationId))
operationId = swaggerOperationAttribute.OperationId;
else
{
operationId = actionDescriptor.ControllerName + "_" + GetActionName(actionDescriptor.ActionName);
}
var number = 1;
while (document.Operations.Any(o => o.Operation.OperationId == operationId + (number > 1 ? "_" + number : string.Empty)))
number++;
return operationId + (number > 1 ? number.ToString() : string.Empty);
}
private static string GetActionName(string actionName)
{
if (actionName.EndsWith("Async"))
actionName = actionName.Substring(0, actionName.Length - 5);
return actionName;
}
}
}