4
4
// ------------------------------------------------------------
5
5
6
6
using System ;
7
- using System . Linq ;
8
7
using System . Collections . Generic ;
8
+ using System . Linq ;
9
9
using Microsoft . OpenApi . Any ;
10
10
using Microsoft . OpenApi . Exceptions ;
11
11
using Microsoft . OpenApi . Interfaces ;
@@ -31,7 +31,7 @@ public static void AddPathItem(this OpenApiDocument document, string name, Actio
31
31
throw Error . ArgumentNull ( nameof ( document ) ) ;
32
32
}
33
33
34
- if ( String . IsNullOrWhiteSpace ( name ) )
34
+ if ( string . IsNullOrWhiteSpace ( name ) )
35
35
{
36
36
throw Error . ArgumentNullOrWhiteSpace ( nameof ( name ) ) ;
37
37
}
@@ -58,7 +58,10 @@ public static void AddPathItem(this OpenApiDocument document, string name, Actio
58
58
/// <param name="pathItem">The Open API path item.</param>
59
59
/// <param name="operationType">The operation type kind.</param>
60
60
/// <param name="configure">The operation configuration action.</param>
61
- public static void AddOperation ( this OpenApiPathItem pathItem , OperationType operationType , Action < OpenApiOperation > configure )
61
+ public static void AddOperation (
62
+ this OpenApiPathItem pathItem ,
63
+ OperationType operationType ,
64
+ Action < OpenApiOperation > configure )
62
65
{
63
66
if ( pathItem == null )
64
67
{
@@ -90,7 +93,7 @@ public static void AddHeader(this OpenApiResponse response, string name, Action<
90
93
throw Error . ArgumentNull ( nameof ( response ) ) ;
91
94
}
92
95
93
- if ( String . IsNullOrWhiteSpace ( name ) )
96
+ if ( string . IsNullOrWhiteSpace ( name ) )
94
97
{
95
98
throw Error . ArgumentNullOrWhiteSpace ( nameof ( name ) ) ;
96
99
}
@@ -100,7 +103,7 @@ public static void AddHeader(this OpenApiResponse response, string name, Action<
100
103
throw Error . ArgumentNull ( nameof ( configure ) ) ;
101
104
}
102
105
103
- OpenApiHeader header = new OpenApiHeader ( ) ;
106
+ var header = new OpenApiHeader ( ) ;
104
107
configure ( header ) ;
105
108
106
109
if ( response . Headers == null )
@@ -124,7 +127,7 @@ public static void AddMediaType(this OpenApiResponse response, string name, Acti
124
127
throw Error . ArgumentNull ( nameof ( response ) ) ;
125
128
}
126
129
127
- if ( String . IsNullOrWhiteSpace ( name ) )
130
+ if ( string . IsNullOrWhiteSpace ( name ) )
128
131
{
129
132
throw Error . ArgumentNullOrWhiteSpace ( nameof ( name ) ) ;
130
133
}
@@ -134,7 +137,7 @@ public static void AddMediaType(this OpenApiResponse response, string name, Acti
134
137
throw Error . ArgumentNull ( nameof ( configure ) ) ;
135
138
}
136
139
137
- OpenApiMediaType mediaType = new OpenApiMediaType ( ) ;
140
+ var mediaType = new OpenApiMediaType ( ) ;
138
141
configure ( mediaType ) ;
139
142
140
143
if ( response . Content == null )
@@ -158,7 +161,7 @@ public static void AddLink(this OpenApiResponse response, string name, Action<Op
158
161
throw Error . ArgumentNull ( nameof ( response ) ) ;
159
162
}
160
163
161
- if ( String . IsNullOrWhiteSpace ( name ) )
164
+ if ( string . IsNullOrWhiteSpace ( name ) )
162
165
{
163
166
throw Error . ArgumentNullOrWhiteSpace ( nameof ( name ) ) ;
164
167
}
@@ -168,7 +171,7 @@ public static void AddLink(this OpenApiResponse response, string name, Action<Op
168
171
throw Error . ArgumentNull ( nameof ( configure ) ) ;
169
172
}
170
173
171
- OpenApiLink link = new OpenApiLink ( ) ;
174
+ var link = new OpenApiLink ( ) ;
172
175
configure ( link ) ;
173
176
174
177
if ( response . Links == null )
@@ -192,7 +195,7 @@ public static void AddResponse(this OpenApiOperation operation, string name, Act
192
195
throw Error . ArgumentNull ( nameof ( operation ) ) ;
193
196
}
194
197
195
- if ( String . IsNullOrWhiteSpace ( name ) )
198
+ if ( string . IsNullOrWhiteSpace ( name ) )
196
199
{
197
200
throw Error . ArgumentNullOrWhiteSpace ( nameof ( name ) ) ;
198
201
}
@@ -213,6 +216,40 @@ public static void AddResponse(this OpenApiOperation operation, string name, Act
213
216
operation . Responses . Add ( name , response ) ;
214
217
}
215
218
219
+ /// <summary>
220
+ /// Add a <see cref="OpenApiSecurityScheme"/> into the <see cref="OpenApiSecurityRequirement"/>.
221
+ /// </summary>
222
+ /// <param name="securityRequirement">The security requirement to add the security scheme to.</param>
223
+ /// <param name="securityScheme">The security scheme to add as key.</param>
224
+ /// <param name="scopes">The scopes array to be used as the value.</param>
225
+ public static void AddSecurityScheme (
226
+ this OpenApiSecurityRequirement securityRequirement ,
227
+ OpenApiSecurityScheme securityScheme ,
228
+ List < string > scopes )
229
+ {
230
+ if ( securityRequirement == null )
231
+ {
232
+ throw Error . ArgumentNull ( nameof ( securityRequirement ) ) ;
233
+ }
234
+
235
+ if ( securityScheme == null )
236
+ {
237
+ throw Error . ArgumentNull ( nameof ( securityScheme ) ) ;
238
+ }
239
+
240
+ if ( scopes == null )
241
+ {
242
+ throw Error . ArgumentNull ( nameof ( scopes ) ) ;
243
+ }
244
+
245
+ if ( securityRequirement . Schemes == null )
246
+ {
247
+ securityRequirement . Schemes = new OpenApiSecuritySchemeDictionary ( ) ;
248
+ }
249
+
250
+ securityRequirement . Schemes . Add ( securityScheme , scopes ) ;
251
+ }
252
+
216
253
/// <summary>
217
254
/// Try find the referenced element.
218
255
/// </summary>
@@ -231,7 +268,7 @@ public static IOpenApiElement Find<T>(this T reference, OpenApiDocument document
231
268
return null ;
232
269
}
233
270
234
- switch ( reference . Reference . Type )
271
+ switch ( reference . Reference . Type )
235
272
{
236
273
case ReferenceType . Schema :
237
274
return document . Components . Schemas ? [ reference . Reference . Id ] ;
@@ -273,11 +310,13 @@ public static IOpenApiElement Find<T>(this T reference, OpenApiDocument document
273
310
/// </summary>
274
311
/// <typeparam name="T"><see cref="IOpenApiElement"/>.</typeparam>
275
312
/// <param name="element">The referencable element.</param>
276
- /// <returns>True if the element implements <see cref="IOpenApiReferenceable"/> and pointer is not null,
277
- /// False otherwise.</returns>
313
+ /// <returns>
314
+ /// True if the element implements <see cref="IOpenApiReferenceable"/> and pointer is not null,
315
+ /// False otherwise.
316
+ /// </returns>
278
317
public static bool IsReference < T > ( this T element ) where T : IOpenApiElement
279
318
{
280
- IOpenApiReferenceable reference = element as IOpenApiReferenceable ;
319
+ var reference = element as IOpenApiReferenceable ;
281
320
return reference ? . Reference != null ;
282
321
}
283
322
@@ -308,15 +347,15 @@ public static void AddExtension<T>(this T element, string name, IOpenApiAny any)
308
347
309
348
private static void VerifyExtensionName ( string name )
310
349
{
311
- if ( String . IsNullOrWhiteSpace ( name ) )
350
+ if ( string . IsNullOrWhiteSpace ( name ) )
312
351
{
313
352
throw Error . ArgumentNullOrWhiteSpace ( nameof ( name ) ) ;
314
353
}
315
354
316
355
if ( ! name . StartsWith ( OpenApiConstants . ExtensionFieldNamePrefix ) )
317
356
{
318
- throw new OpenApiException ( String . Format ( SRResource . ExtensionFieldNameMustBeginWithXDash , name ) ) ;
357
+ throw new OpenApiException ( string . Format ( SRResource . ExtensionFieldNameMustBeginWithXDash , name ) ) ;
319
358
}
320
359
}
321
360
}
322
- }
361
+ }
0 commit comments