-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
SwaggerGenerator.cs
467 lines (398 loc) · 20 KB
/
SwaggerGenerator.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Any;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Swagger;
using System.ComponentModel;
namespace Swashbuckle.AspNetCore.SwaggerGen
{
public class SwaggerGenerator : ISwaggerProvider
{
private readonly IApiDescriptionGroupCollectionProvider _apiDescriptionsProvider;
private readonly ISchemaGenerator _schemaGenerator;
private readonly SwaggerGeneratorOptions _options;
public SwaggerGenerator(
IApiDescriptionGroupCollectionProvider apiDescriptionsProvider,
ISchemaGenerator schemaGenerator,
IOptions<SwaggerGeneratorOptions> optionsAccessor)
: this(apiDescriptionsProvider, schemaGenerator, optionsAccessor.Value)
{ }
public SwaggerGenerator(
IApiDescriptionGroupCollectionProvider apiDescriptionsProvider,
ISchemaGenerator schemaGenerator,
SwaggerGeneratorOptions options)
{
_apiDescriptionsProvider = apiDescriptionsProvider;
_schemaGenerator = schemaGenerator;
_options = options ?? new SwaggerGeneratorOptions();
}
public OpenApiDocument GetSwagger(string documentName, string host = null, string basePath = null)
{
if (!_options.SwaggerDocs.TryGetValue(documentName, out OpenApiInfo info))
throw new UnknownSwaggerDocument(documentName, _options.SwaggerDocs.Select(d => d.Key));
var applicableApiDescriptions = _apiDescriptionsProvider.ApiDescriptionGroups.Items
.SelectMany(group => group.Items)
.Where(apiDesc => _options.DocInclusionPredicate(documentName, apiDesc))
.Where(apiDesc => !_options.IgnoreObsoleteActions || !apiDesc.IsObsolete());
var schemaRepository = new SchemaRepository();
var swaggerDoc = new OpenApiDocument
{
Info = info,
Servers = GenerateServers(host, basePath),
Paths = GeneratePaths(applicableApiDescriptions, schemaRepository),
Components = new OpenApiComponents
{
Schemas = schemaRepository.Schemas,
SecuritySchemes = _options.SecuritySchemes
},
SecurityRequirements = _options.SecurityRequirements
};
var filterContext = new DocumentFilterContext(applicableApiDescriptions, _schemaGenerator, schemaRepository);
foreach (var filter in _options.DocumentFilters)
{
filter.Apply(swaggerDoc, filterContext);
}
return swaggerDoc;
}
private IList<OpenApiServer> GenerateServers(string host, string basePath)
{
return (host == null && basePath == null)
? new List<OpenApiServer>()
: new List<OpenApiServer> { new OpenApiServer { Url = $"{host}{basePath}" } };
}
private OpenApiPaths GeneratePaths(IEnumerable<ApiDescription> apiDescriptions, SchemaRepository schemaRepository)
{
var apiDescriptionsByPath = apiDescriptions
.OrderBy(_options.SortKeySelector)
.GroupBy(apiDesc => apiDesc.RelativePathSansQueryString());
var paths = new OpenApiPaths();
foreach (var group in apiDescriptionsByPath)
{
paths.Add($"/{group.Key}", GeneratePathItem(group, schemaRepository));
};
return paths;
}
private OpenApiPathItem GeneratePathItem(IEnumerable<ApiDescription> apiDescriptions, SchemaRepository schemaRepository)
{
return new OpenApiPathItem
{
Operations = GenerateOperations(apiDescriptions, schemaRepository)
};
}
private IDictionary<OperationType, OpenApiOperation> GenerateOperations(
IEnumerable<ApiDescription> apiDescriptions,
SchemaRepository schemaRepository)
{
var apiDescriptionsByMethod = apiDescriptions
.OrderBy(_options.SortKeySelector)
.GroupBy(apiDesc => apiDesc.HttpMethod);
var operations = new Dictionary<OperationType, OpenApiOperation>();
foreach (var group in apiDescriptionsByMethod)
{
var httpMethod = group.Key;
if (httpMethod == null)
throw new NotSupportedException(string.Format(
"Ambiguous HTTP method for action - {0}. " +
"Actions require an explicit HttpMethod binding for Swagger 2.0",
group.First().ActionDescriptor.DisplayName));
if (group.Count() > 1 && _options.ConflictingActionsResolver == null)
throw new NotSupportedException(string.Format(
"HTTP method \"{0}\" & path \"{1}\" overloaded by actions - {2}. " +
"Actions require unique method/path combination for OpenAPI 3.0. Use ConflictingActionsResolver as a workaround",
httpMethod,
group.First().RelativePathSansQueryString(),
string.Join(",", group.Select(apiDesc => apiDesc.ActionDescriptor.DisplayName))));
var apiDescription = (group.Count() > 1) ? _options.ConflictingActionsResolver(group) : group.Single();
operations.Add(OperationTypeMap[httpMethod.ToUpper()], GenerateOperation(apiDescription, schemaRepository));
};
return operations;
}
private OpenApiOperation GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository)
{
apiDescription.GetAdditionalMetadata(out MethodInfo methodInfo, out IEnumerable<object> methodAttributes);
var operation = new OpenApiOperation
{
Tags = GenerateTags(apiDescription),
OperationId = _options.OperationIdSelector(apiDescription),
Parameters = GenerateParameters(apiDescription, schemaRepository),
RequestBody = GenerateRequestBody(apiDescription, methodAttributes, schemaRepository),
Responses = GenerateResponses(apiDescription, methodAttributes, schemaRepository),
Deprecated = methodAttributes.OfType<ObsoleteAttribute>().Any()
};
var filterContext = new OperationFilterContext(apiDescription, _schemaGenerator, schemaRepository, methodInfo);
foreach (var filter in _options.OperationFilters)
{
filter.Apply(operation, filterContext);
}
return operation;
}
private IList<OpenApiTag> GenerateTags(ApiDescription apiDescription)
{
return _options.TagsSelector(apiDescription)
.Select(tagName => new OpenApiTag { Name = tagName })
.ToList();
}
private IList<OpenApiParameter> GenerateParameters(ApiDescription apiDescription, SchemaRepository schemaRespository)
{
var applicableApiParameters = apiDescription.ParameterDescriptions
.Where(apiParam =>
{
return (!apiParam.IsFromBody() && !apiParam.IsFromForm())
&& (apiParam.ModelMetadata == null || apiParam.ModelMetadata.IsBindingAllowed);
});
return applicableApiParameters
.Select(paramDesc => GenerateParameter(apiDescription, paramDesc, schemaRespository))
.ToList();
}
private OpenApiParameter GenerateParameter(
ApiDescription apiDescription,
ApiParameterDescription apiParameter,
SchemaRepository schemaRepository)
{
apiParameter.GetAdditionalMetadata(
apiDescription,
out ParameterInfo parameterInfo,
out PropertyInfo propertyInfo,
out IEnumerable<object> parameterOrPropertyAttributes);
var name = _options.DescribeAllParametersInCamelCase
? apiParameter.Name.ToCamelCase()
: apiParameter.Name;
var location = ParameterLocationMap.ContainsKey(apiParameter.Source)
? ParameterLocationMap[apiParameter.Source]
: ParameterLocation.Query;
var isRequired = (apiParameter.IsFromPath())
|| parameterOrPropertyAttributes.Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));
var defaultValue = parameterInfo?.DefaultValue
?? parameterOrPropertyAttributes.OfType<DefaultValueAttribute>().FirstOrDefault()?.Value;
var schema = (apiParameter.ModelMetadata != null)
? _schemaGenerator.GenerateSchema(apiParameter.Type, schemaRepository)
: new OpenApiSchema { Type = "string" };
if (defaultValue != null && schema.Reference == null)
{
schema.Default = OpenApiAnyFactory.TryCreateFrom(defaultValue, out IOpenApiAny openApiAny)
? openApiAny
: null;
}
var parameter = new OpenApiParameter
{
Name = name,
In = location,
Required = isRequired,
Schema = schema
};
var filterContext = new ParameterFilterContext(apiParameter, _schemaGenerator, schemaRepository, parameterInfo, propertyInfo);
foreach (var filter in _options.ParameterFilters)
{
filter.Apply(parameter, filterContext);
}
return parameter;
}
private OpenApiRequestBody GenerateRequestBody(
ApiDescription apiDescription,
IEnumerable<object> methodAttributes,
SchemaRepository schemaRepository)
{
var requestContentTypes = InferRequestContentTypes(apiDescription, methodAttributes);
if (!requestContentTypes.Any()) return null;
return new OpenApiRequestBody
{
Content = requestContentTypes
.ToDictionary(
contentType => contentType,
contentType => GenerateRequestMediaType(contentType, apiDescription, schemaRepository)
)
};
}
private IEnumerable<string> InferRequestContentTypes(ApiDescription apiDescription, IEnumerable<object> methodAttributes)
{
// If there's no api parameters from body or form, return an empty list (i.e. no content)
if (!apiDescription.ParameterDescriptions.Any(apiParam => apiParam.IsFromBody() || apiParam.IsFromForm()))
return Enumerable.Empty<string>();
// If there's content types explicitly specified via ConsumesAttribute, use them
var explicitContentTypes = methodAttributes.OfType<ConsumesAttribute>()
.SelectMany(attr => attr.ContentTypes)
.Distinct();
if (explicitContentTypes.Any()) return explicitContentTypes;
// If there's content types surfaced by ApiExplorer, use them
var apiExplorerContentTypes = apiDescription.SupportedRequestFormats
.Select(format => format.MediaType)
.Distinct();
if (apiExplorerContentTypes.Any()) return apiExplorerContentTypes;
// As a last resort, try to infer from parameter bindings
return apiDescription.ParameterDescriptions.Any(paramDesc => paramDesc.IsFromForm())
? new[] { "multipart/form-data" }
: Enumerable.Empty<string>();
}
private OpenApiMediaType GenerateRequestMediaType(
string contentType,
ApiDescription apiDescription,
SchemaRepository schemaRepository)
{
// If there's form parameters, generate the form-flavoured media type
var apiParametersFromForm = apiDescription.ParameterDescriptions
.Where(paramDesc => paramDesc.IsFromForm());
if (apiParametersFromForm.Any())
{
var schema = GenerateSchemaFromApiParameters(apiDescription, apiParametersFromForm, schemaRepository);
// Provide schema and corresponding encoding map to specify "form" serialization style for all properties
// This indicates that array properties must be submitted as multiple parameters with the same name.
// NOTE: the swagger-ui doesn't currently support arrays of files - https://github.com/swagger-api/swagger-ui/issues/4600
// NOTE: the swagger-ui doesn't currently honor encoding metadata - https://github.com/swagger-api/swagger-ui/issues/4836
return new OpenApiMediaType
{
Schema = schema,
Encoding = schema.Properties.ToDictionary(
entry => entry.Key,
entry => new OpenApiEncoding { Style = ParameterStyle.Form }
)
};
}
// Otherwise, generate a regular media type
var apiParameterFromBody = apiDescription.ParameterDescriptions
.First(paramDesc => paramDesc.IsFromBody());
return new OpenApiMediaType
{
Schema = _schemaGenerator.GenerateSchema(apiParameterFromBody.Type, schemaRepository)
};
}
private OpenApiSchema GenerateSchemaFromApiParameters(
ApiDescription apiDescription,
IEnumerable<ApiParameterDescription> apiParameters,
SchemaRepository schemaRepository)
{
// First, map to a data structure that captures the pertinent metadata
var parametersMetadata = apiParameters
.Select(paramDesc =>
{
paramDesc.GetAdditionalMetadata(
apiDescription,
out ParameterInfo parameterInfo,
out PropertyInfo propertyInfo,
out IEnumerable<object> parameterOrPropertyAttributes);
var name = _options.DescribeAllParametersInCamelCase ? paramDesc.Name.ToCamelCase() : paramDesc.Name;
var isRequired = parameterOrPropertyAttributes.Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));
var schema = _schemaGenerator.GenerateSchema(paramDesc.Type, schemaRepository);
return new
{
Name = name,
IsRequired = isRequired,
Schema = schema
};
});
return new OpenApiSchema
{
Type = "object",
Properties = parametersMetadata.ToDictionary(
metadata => metadata.Name,
metadata => metadata.Schema
),
Required = new SortedSet<string>(parametersMetadata.Where(m => m.IsRequired).Select(m => m.Name)),
};
}
private OpenApiResponses GenerateResponses(
ApiDescription apiDescription,
IEnumerable<object> methodAttributes,
SchemaRepository schemaRepository)
{
var supportedResponseTypes = apiDescription.SupportedResponseTypes
.DefaultIfEmpty(new ApiResponseType { StatusCode = 200 });
var responses = new OpenApiResponses();
foreach (var responseType in supportedResponseTypes)
{
var statusCode = responseType.IsDefaultResponse() ? "default" : responseType.StatusCode.ToString();
responses.Add(statusCode, GenerateResponse(statusCode, responseType, methodAttributes, schemaRepository));
}
return responses;
}
private OpenApiResponse GenerateResponse(
string statusCode,
ApiResponseType apiResponseType,
IEnumerable<object> methodAttributes,
SchemaRepository schemaRepository)
{
var description = ResponseDescriptionMap
.FirstOrDefault((entry) => Regex.IsMatch(statusCode, entry.Key))
.Value;
var responseContentTypes = InferResponseContentTypes(apiResponseType, methodAttributes);
return new OpenApiResponse
{
Description = description,
Content = responseContentTypes.ToDictionary(
contentType => contentType,
contentType => CreateResponseMediaType(apiResponseType.Type, schemaRepository)
)
};
}
private IEnumerable<string> InferResponseContentTypes(ApiResponseType apiResponseType, IEnumerable<object> methodAttributes)
{
// If there's no associated model, return an empty list (i.e. no content)
if (apiResponseType.ModelMetadata == null) return Enumerable.Empty<string>();
// If there's content types explicitly specified via ProducesAttribute, use them
var explicitContentTypes = methodAttributes.OfType<ProducesAttribute>()
.SelectMany(attr => attr.ContentTypes)
.Distinct();
if (explicitContentTypes.Any()) return explicitContentTypes;
// If there's content types surfaced by ApiExplorer, use them
var apiExplorerContentTypes = apiResponseType.ApiResponseFormats
.Select(responseFormat => responseFormat.MediaType)
.Distinct();
if (apiExplorerContentTypes.Any()) return apiExplorerContentTypes;
return Enumerable.Empty<string>();
}
private OpenApiMediaType CreateResponseMediaType(Type type, SchemaRepository schemaRespository)
{
return new OpenApiMediaType
{
Schema = _schemaGenerator.GenerateSchema(type, schemaRespository)
};
}
private static readonly Dictionary<string, OperationType> OperationTypeMap = new Dictionary<string, OperationType>
{
{ "GET", OperationType.Get },
{ "PUT", OperationType.Put },
{ "POST", OperationType.Post },
{ "DELETE", OperationType.Delete },
{ "OPTIONS", OperationType.Options },
{ "HEAD", OperationType.Head },
{ "PATCH", OperationType.Patch },
{ "TRACE", OperationType.Trace }
};
private static Dictionary<BindingSource, ParameterLocation> ParameterLocationMap = new Dictionary<BindingSource, ParameterLocation>
{
{ BindingSource.Query, ParameterLocation.Query },
{ BindingSource.Header, ParameterLocation.Header },
{ BindingSource.Path, ParameterLocation.Path }
};
private static IEnumerable<Type> RequiredAttributeTypes = new[]
{
typeof(BindRequiredAttribute),
typeof(RequiredAttribute)
};
private static readonly Dictionary<string, string> ResponseDescriptionMap = new Dictionary<string, string>
{
{ "1\\d{2}", "Information" },
{ "2\\d{2}", "Success" },
{ "304", "Not Modified" },
{ "3\\d{2}", "Redirect" },
{ "400", "Bad Request" },
{ "401", "Unauthorized" },
{ "403", "Forbidden" },
{ "404", "Not Found" },
{ "405", "Method Not Allowed" },
{ "406", "Not Acceptable" },
{ "408", "Request Timeout" },
{ "409", "Conflict" },
{ "4\\d{2}", "Client Error" },
{ "5\\d{2}", "Server Error" },
{ "default", "Unexpected Error" }
};
}
}