-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
SwaggerGenOptionsExtensions.cs
307 lines (284 loc) · 13.4 KB
/
SwaggerGenOptionsExtensions.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
using System;
using System.Collections.Generic;
using System.Xml.XPath;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Microsoft.Extensions.DependencyInjection
{
public static class SwaggerGenOptionsExtensions
{
/// <summary>
/// Define one or more documents to be created by the Swagger generator
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="name">A URI-friendly name that uniquely identifies the document</param>
/// <param name="info">Global metadata to be included in the Swagger output</param>
public static void SwaggerDoc(
this SwaggerGenOptions swaggerGenOptions,
string name,
Info info)
{
swaggerGenOptions.SwaggerGeneratorOptions.SwaggerDocs.Add(name, info);
}
/// <summary>
/// Provide a custom strategy for selecting actions.
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="predicate">
/// A lambda that returns true/false based on document name and ApiDescription
/// </param>
public static void DocInclusionPredicate(
this SwaggerGenOptions swaggerGenOptions,
Func<string, ApiDescription, bool> predicate)
{
swaggerGenOptions.SwaggerGeneratorOptions.DocInclusionPredicate = predicate;
}
/// <summary>
/// Ignore any actions that are decorated with the ObsoleteAttribute
/// </summary>
public static void IgnoreObsoleteActions(this SwaggerGenOptions swaggerGenOptions)
{
swaggerGenOptions.SwaggerGeneratorOptions.IgnoreObsoleteActions = true;
}
/// <summary>
/// Merge actions that have conflicting HTTP methods and paths (must be unique for Swagger 2.0)
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="resolver"></param>
public static void ResolveConflictingActions(
this SwaggerGenOptions swaggerGenOptions,
Func<IEnumerable<ApiDescription>, ApiDescription> resolver)
{
swaggerGenOptions.SwaggerGeneratorOptions.ConflictingActionsResolver = resolver;
}
/// <summary>
/// Provide a custom strategy for assigning a default "tag" to actions
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="tagSelector"></param>
public static void TagActionsBy(
this SwaggerGenOptions swaggerGenOptions,
Func<ApiDescription, string> tagSelector)
{
swaggerGenOptions.SwaggerGeneratorOptions.TagSelector = tagSelector;
}
/// <summary>
/// Provide a custom strategy for sorting actions before they're transformed into the Swagger format
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="sortKeySelector"></param>
public static void OrderActionsBy(
this SwaggerGenOptions swaggerGenOptions,
Func<ApiDescription, string> sortKeySelector)
{
swaggerGenOptions.SwaggerGeneratorOptions.SortKeySelector = sortKeySelector;
}
/// <summary>
/// Describe all parameters, regardless of how they appear in code, in camelCase
/// </summary>
public static void DescribeAllParametersInCamelCase(this SwaggerGenOptions swaggerGenOptions)
{
swaggerGenOptions.SwaggerGeneratorOptions.DescribeAllParametersInCamelCase = true;
}
/// <summary>
/// Add one or more "securityDefinitions", describing how your API is protected, to the generated Swagger
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="name">A unique name for the scheme, as per the Swagger spec.</param>
/// <param name="securityScheme">
/// A description of the scheme - can be an instance of BasicAuthScheme, ApiKeyScheme or OAuth2Scheme
/// </param>
public static void AddSecurityDefinition(
this SwaggerGenOptions swaggerGenOptions,
string name,
SecurityScheme securityScheme)
{
swaggerGenOptions.SwaggerGeneratorOptions.SecurityDefinitions.Add(name, securityScheme);
}
/// <summary>
/// Adds a global security requirement
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="requirement">
/// A dictionary of required schemes (logical AND). Keys must correspond to schemes defined through AddSecurityDefinition
/// If the scheme is of type "oauth2", then the value is a list of scopes, otherwise it MUST be an empty array
/// </param>
public static void AddSecurityRequirement(
this SwaggerGenOptions swaggerGenOptions,
IDictionary<string, IEnumerable<string>> requirement)
{
swaggerGenOptions.SwaggerGeneratorOptions.SecurityRequirements.Add(requirement);
}
/// <summary>
/// Provide a custom mapping, for a given type, to the Swagger-flavored JSONSchema
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="type">System type</param>
/// <param name="schemaFactory">A factory method that generates Schema's for the provided type</param>
public static void MapType(
this SwaggerGenOptions swaggerGenOptions,
Type type,
Func<Schema> schemaFactory)
{
swaggerGenOptions.SchemaRegistryOptions.CustomTypeMappings.Add(type, schemaFactory);
}
/// <summary>
/// Provide a custom mapping, for a given type, to the Swagger-flavored JSONSchema
/// </summary>
/// <typeparam name="T">System type</typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="schemaFactory">A factory method that generates Schema's for the provided type</param>
public static void MapType<T>(
this SwaggerGenOptions swaggerGenOptions,
Func<Schema> schemaFactory)
{
swaggerGenOptions.MapType(typeof(T), schemaFactory);
}
/// <summary>
/// Use the enum names, as opposed to their integer values, when describing enum types
/// </summary>
public static void DescribeAllEnumsAsStrings(this SwaggerGenOptions swaggerGenOptions)
{
swaggerGenOptions.SchemaRegistryOptions.DescribeAllEnumsAsStrings = true;
}
/// <summary>
/// If applicable, describe all enum names, regardless of how they appear in code, in camelCase.
/// </summary>
public static void DescribeStringEnumsInCamelCase(this SwaggerGenOptions swaggerGenOptions)
{
swaggerGenOptions.SchemaRegistryOptions.DescribeStringEnumsInCamelCase = true;
}
/// <summary>
/// Use referenced definitions for enum types within body parameter and response schemas
/// </summary>
public static void UseReferencedDefinitionsForEnums(this SwaggerGenOptions swaggerGenOptions)
{
swaggerGenOptions.SchemaRegistryOptions.UseReferencedDefinitionsForEnums = true;
}
/// <summary>
/// Provide a custom strategy for generating the unique Id's that are used to reference object Schema's
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="schemaIdSelector">
/// A lambda that returns a unique identifier for the provided system type
/// </param>
public static void CustomSchemaIds(
this SwaggerGenOptions swaggerGenOptions,
Func<Type, string> schemaIdSelector)
{
swaggerGenOptions.SchemaRegistryOptions.SchemaIdSelector = schemaIdSelector;
}
/// <summary>
/// Ignore any properties that are decorated with the ObsoleteAttribute
/// </summary>
public static void IgnoreObsoleteProperties(this SwaggerGenOptions swaggerGenOptions)
{
swaggerGenOptions.SchemaRegistryOptions.IgnoreObsoleteProperties = true;
}
/// <summary>
/// Extend the Swagger Generator with "filters" that can modify Schemas after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from ISchemaFilter</typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="arguments">Optionally inject parameters through filter constructors</param>
public static void SchemaFilter<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
params object[] arguments)
where TFilter : ISchemaFilter
{
swaggerGenOptions.SchemaFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
Arguments = arguments
});
}
/// <summary>
/// Extend the Swagger Generator with "filters" that can modify Parameters after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from IParameterFilter</typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="arguments">Optionally inject parameters through filter constructors</param>
public static void ParameterFilter<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
params object[] arguments)
where TFilter : IParameterFilter
{
swaggerGenOptions.ParameterFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
Arguments = arguments
});
}
/// <summary>
/// Extend the Swagger Generator with "filters" that can modify Operations after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from IOperationFilter</typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="arguments">Optionally inject parameters through filter constructors</param>
public static void OperationFilter<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
params object[] arguments)
where TFilter : IOperationFilter
{
swaggerGenOptions.OperationFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
Arguments = arguments
});
}
/// <summary>
/// Extend the Swagger Generator with "filters" that can modify SwaggerDocuments after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from IDocumentFilter</typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="arguments">Optionally inject parameters through filter constructors</param>
public static void DocumentFilter<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
params object[] arguments)
where TFilter : IDocumentFilter
{
swaggerGenOptions.DocumentFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
Arguments = arguments
});
}
/// <summary>
/// Inject human-friendly descriptions for Operations, Parameters and Schemas based on XML Comment files
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="xmlDocFactory">A factory method that returns XML Comments as an XPathDocument</param>
/// <param name="includeControllerXmlComments">
/// Flag to indicate if controller XML comments (i.e. summary) should be used to assign Tag descriptions.
/// Don't set this flag if you're customizing the default tag for operations via TagActionsBy.
/// </param>
public static void IncludeXmlComments(
this SwaggerGenOptions swaggerGenOptions,
Func<XPathDocument> xmlDocFactory,
bool includeControllerXmlComments = false)
{
var xmlDoc = xmlDocFactory();
swaggerGenOptions.OperationFilter<XmlCommentsOperationFilter>(xmlDoc);
swaggerGenOptions.SchemaFilter<XmlCommentsSchemaFilter>(xmlDoc);
if (includeControllerXmlComments)
swaggerGenOptions.DocumentFilter<XmlCommentsDocumentFilter>(xmlDoc);
}
/// <summary>
/// Inject human-friendly descriptions for Operations, Parameters and Schemas based on XML Comment files
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="filePath">An abolsute path to the file that contains XML Comments</param>
/// <param name="includeControllerXmlComments">
/// Flag to indicate if controller XML comments (i.e. summary) should be used to assign Tag descriptions.
/// Don't set this flag if you're customizing the default tag for operations via TagActionsBy.
/// </param>
public static void IncludeXmlComments(
this SwaggerGenOptions swaggerGenOptions,
string filePath,
bool includeControllerXmlComments = false)
{
swaggerGenOptions.IncludeXmlComments(() => new XPathDocument(filePath), includeControllerXmlComments);
}
}
}