forked from domaindrivendev/Swashbuckle.AspNetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwaggerUIOptionsExtensions.cs
More file actions
337 lines (306 loc) · 13.8 KB
/
Copy pathSwaggerUIOptionsExtensions.cs
File metadata and controls
337 lines (306 loc) · 13.8 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
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
using System;
using System.Collections.Generic;
using System.Text;
using Swashbuckle.AspNetCore.SwaggerUI;
namespace Microsoft.AspNetCore.Builder
{
public static class SwaggerUIOptionsExtensions
{
/// <summary>
/// Injects additional CSS stylesheets into the index.html page
/// </summary>
/// <param name="options"></param>
/// <param name="path">A path to the stylesheet - i.e. the link "href" attribute</param>
/// <param name="media">The target media - i.e. the link "media" attribute</param>
public static void InjectStylesheet(this SwaggerUIOptions options, string path, string media = "screen")
{
var builder = new StringBuilder(options.HeadContent);
builder.AppendLine($"<link href='{path}' rel='stylesheet' media='{media}' type='text/css' />");
options.HeadContent = builder.ToString();
}
/// <summary>
/// Injects additional Javascript files into the index.html page
/// </summary>
/// <param name="options"></param>
/// <param name="path">A path to the javascript - i.e. the script "src" attribute</param>
/// <param name="type">The script type - i.e. the script "type" attribute</param>
public static void InjectJavascript(this SwaggerUIOptions options, string path, string type = "text/javascript")
{
var builder = new StringBuilder(options.HeadContent);
builder.AppendLine($"<script src='{path}' type='{type}'></script>");
options.HeadContent = builder.ToString();
}
/// <summary>
/// Adds Swagger JSON endpoints. Can be fully-qualified or relative to the UI page
/// </summary>
/// <param name="options"></param>
/// <param name="url">Can be fully qualified or relative to the current host</param>
/// <param name="name">The description that appears in the document selector drop-down</param>
public static void SwaggerEndpoint(this SwaggerUIOptions options, string url, string name)
{
var urls = new List<UrlDescriptor>(options.ConfigObject.Urls ?? [])
{
new() { Url = url, Name = name }
};
options.ConfigObject.Urls = urls;
}
/// <summary>
/// Enables deep linking for tags and operations
/// </summary>
/// <param name="options"></param>
public static void EnableDeepLinking(this SwaggerUIOptions options)
{
options.ConfigObject.DeepLinking = true;
}
/// <summary>
/// Enables persist authorization data
/// </summary>
/// <param name="options"></param>
public static void EnablePersistAuthorization(this SwaggerUIOptions options)
{
options.ConfigObject.PersistAuthorization = true;
}
/// <summary>
/// Controls the display of operationId in operations list
/// </summary>
/// <param name="options"></param>
public static void DisplayOperationId(this SwaggerUIOptions options)
{
options.ConfigObject.DisplayOperationId = true;
}
/// <summary>
/// The default expansion depth for models (set to -1 completely hide the models)
/// </summary>
/// <param name="options"></param>
/// <param name="depth"></param>
public static void DefaultModelsExpandDepth(this SwaggerUIOptions options, int depth)
{
options.ConfigObject.DefaultModelsExpandDepth = depth;
}
/// <summary>
/// The default expansion depth for the model on the model-example section
/// </summary>
/// <param name="options"></param>
/// <param name="depth"></param>
public static void DefaultModelExpandDepth(this SwaggerUIOptions options, int depth)
{
options.ConfigObject.DefaultModelExpandDepth = depth;
}
/// <summary>
/// Controls how the model is shown when the API is first rendered.
/// (The user can always switch the rendering for a given model by clicking the 'Model' and 'Example Value' links.)
/// </summary>
/// <param name="options"></param>
/// <param name="modelRendering"></param>
public static void DefaultModelRendering(this SwaggerUIOptions options, ModelRendering modelRendering)
{
options.ConfigObject.DefaultModelRendering = modelRendering;
}
/// <summary>
/// Controls the display of the request duration (in milliseconds) for Try-It-Out requests
/// </summary>
/// <param name="options"></param>
public static void DisplayRequestDuration(this SwaggerUIOptions options)
{
options.ConfigObject.DisplayRequestDuration = true;
}
/// <summary>
/// Controls the default expansion setting for the operations and tags.
/// It can be 'List' (expands only the tags), 'Full' (expands the tags and operations) or 'None' (expands nothing)
/// </summary>
/// <param name="options"></param>
/// <param name="docExpansion"></param>
public static void DocExpansion(this SwaggerUIOptions options, DocExpansion docExpansion)
{
options.ConfigObject.DocExpansion = docExpansion;
}
/// <summary>
/// Enables filtering. The top bar will show an edit box that you can use to filter the tagged operations that are shown.
/// If an expression is provided it will be used and applied initially.
/// Filtering is case sensitive matching the filter expression anywhere inside the tag
/// </summary>
/// <param name="options"></param>
/// <param name="expression"></param>
public static void EnableFilter(this SwaggerUIOptions options, string expression = null)
{
options.ConfigObject.Filter = expression ?? "";
}
/// <summary>
/// Enables the "Try it out" section by default.
/// </summary>
/// <param name="options"></param>
public static void EnableTryItOutByDefault(this SwaggerUIOptions options)
{
options.ConfigObject.TryItOutEnabled = true;
}
/// <summary>
/// Limits the number of tagged operations displayed to at most this many. The default is to show all operations
/// </summary>
/// <param name="options"></param>
/// <param name="count"></param>
public static void MaxDisplayedTags(this SwaggerUIOptions options, int count)
{
options.ConfigObject.MaxDisplayedTags = count;
}
/// <summary>
/// Controls the display of vendor extension (x-) fields and values for Operations, Parameters, and Schema
/// </summary>
/// <param name="options"></param>
public static void ShowExtensions(this SwaggerUIOptions options)
{
options.ConfigObject.ShowExtensions = true;
}
/// <summary>
/// Controls the display of extensions (pattern, maxLength, minLength, maximum, minimum) fields and values for Parameters
/// </summary>
/// <param name="options"></param>
public static void ShowCommonExtensions(this SwaggerUIOptions options)
{
options.ConfigObject.ShowCommonExtensions = true;
}
/// <summary>
/// List of HTTP methods that have the Try it out feature enabled. An empty array disables Try it out for all operations.
/// This does not filter the operations from the display
/// </summary>
/// <param name="options"></param>
/// <param name="submitMethods"></param>
public static void SupportedSubmitMethods(this SwaggerUIOptions options, params SubmitMethod[] submitMethods)
{
options.ConfigObject.SupportedSubmitMethods = submitMethods;
}
/// <summary>
/// OAuth redirect URL
/// </summary>
/// <param name="options"></param>
/// <param name="url"></param>
public static void OAuth2RedirectUrl(this SwaggerUIOptions options, string url)
{
options.ConfigObject.OAuth2RedirectUrl = url;
}
[Obsolete("The validator is disabled by default. Use EnableValidator to enable it")]
public static void ValidatorUrl(this SwaggerUIOptions options, string url)
{
options.ConfigObject.ValidatorUrl = url;
}
/// <summary>
/// You can use this parameter to enable the swagger-ui's built-in validator (badge) functionality
/// Setting it to null will disable validation
/// </summary>
/// <param name="options"></param>
/// <param name="url"></param>
public static void EnableValidator(this SwaggerUIOptions options, string url = "https://online.swagger.io/validator")
{
options.ConfigObject.ValidatorUrl = url;
}
/// <summary>
/// Default clientId
/// </summary>
/// <param name="options"></param>
/// <param name="value"></param>
public static void OAuthClientId(this SwaggerUIOptions options, string value)
{
options.OAuthConfigObject.ClientId = value;
}
/// <summary>
/// Default userName
/// </summary>
/// <param name="options"></param>
/// <param name="value"></param>
public static void OAuthUsername(this SwaggerUIOptions options, string value)
{
options.OAuthConfigObject.Username = value;
}
/// <summary>
/// Default clientSecret
/// </summary>
/// <param name="options"></param>
/// <param name="value"></param>
/// <remarks>Setting this exposes the client secrets in inline javascript in the swagger-ui generated html.</remarks>
public static void OAuthClientSecret(this SwaggerUIOptions options, string value)
{
options.OAuthConfigObject.ClientSecret = value;
}
/// <summary>
/// realm query parameter (for oauth1) added to authorizationUrl and tokenUrl
/// </summary>
/// <param name="options"></param>
/// <param name="value"></param>
public static void OAuthRealm(this SwaggerUIOptions options, string value)
{
options.OAuthConfigObject.Realm = value;
}
/// <summary>
/// Application name, displayed in authorization popup
/// </summary>
/// <param name="options"></param>
/// <param name="value"></param>
public static void OAuthAppName(this SwaggerUIOptions options, string value)
{
options.OAuthConfigObject.AppName = value;
}
/// <summary>
/// Scope separator for passing scopes, encoded before calling, default value is a space (encoded value %20)
/// </summary>
/// <param name="options"></param>
/// <param name="value"></param>
public static void OAuthScopeSeparator(this SwaggerUIOptions options, string value)
{
options.OAuthConfigObject.ScopeSeparator = value;
}
/// <summary>
/// String array of initially selected oauth scopes, default is empty array
/// </summary>
public static void OAuthScopes(this SwaggerUIOptions options, params string[] scopes)
{
options.OAuthConfigObject.Scopes = scopes;
}
/// <summary>
/// Additional query parameters added to authorizationUrl and tokenUrl
/// </summary>
/// <param name="options"></param>
/// <param name="value"></param>
public static void OAuthAdditionalQueryStringParams(
this SwaggerUIOptions options,
Dictionary<string, string> value)
{
options.OAuthConfigObject.AdditionalQueryStringParams = value;
}
/// <summary>
/// Only activated for the accessCode flow. During the authorization_code request to the tokenUrl,
/// pass the Client Password using the HTTP Basic Authentication scheme (Authorization header with
/// Basic base64encoded[client_id:client_secret]). The default is false
/// </summary>
/// <param name="options"></param>
public static void OAuthUseBasicAuthenticationWithAccessCodeGrant(this SwaggerUIOptions options)
{
options.OAuthConfigObject.UseBasicAuthenticationWithAccessCodeGrant = true;
}
/// <summary>
/// Only applies to authorizatonCode flows. Proof Key for Code Exchange brings enhanced security for OAuth public clients.
/// The default is false
/// </summary>
/// <param name="options"></param>
public static void OAuthUsePkce(this SwaggerUIOptions options)
{
options.OAuthConfigObject.UsePkceWithAuthorizationCodeGrant = true;
}
/// <summary>
/// Function to intercept remote definition, "Try it out", and OAuth 2.0 requests.
/// </summary>
/// <param name="options"></param>
/// <param name="value">MUST be a valid Javascript function: (request: SwaggerRequest) => SwaggerRequest</param>
public static void UseRequestInterceptor(this SwaggerUIOptions options, string value)
{
options.Interceptors.RequestInterceptorFunction = value;
}
/// <summary>
/// Function to intercept remote definition, "Try it out", and OAuth 2.0 responses.
/// </summary>
/// <param name="options"></param>
/// <param name="value">MUST be a valid Javascript function: (response: SwaggerResponse ) => SwaggerResponse </param>
public static void UseResponseInterceptor(this SwaggerUIOptions options, string value)
{
options.Interceptors.ResponseInterceptorFunction = value;
}
}
}