@@ -108,6 +108,7 @@ pub struct Transpiler<'a> {
108
108
struct TranspileState < ' a > {
109
109
oas : & ' a mut openapi3:: Spec ,
110
110
operation_ids : & ' a mut BTreeMap < String , usize > ,
111
+ auth_stack : & ' a mut Vec < SecurityRequirement > ,
111
112
hierarchy : & ' a mut Vec < String > ,
112
113
}
113
114
@@ -156,6 +157,7 @@ impl<'a> Transpiler<'a> {
156
157
oas : & mut oas,
157
158
operation_ids : & mut operation_ids,
158
159
hierarchy : & mut hierarchy,
160
+ auth_stack : & mut Vec :: < SecurityRequirement > :: new ( ) ,
159
161
} ;
160
162
161
163
let transpiler = Transpiler {
@@ -189,7 +191,7 @@ impl<'a> Transpiler<'a> {
189
191
} ;
190
192
let description = extract_description ( & item. description ) ;
191
193
192
- self . transform_folder ( state, i, name, description) ;
194
+ self . transform_folder ( state, i, name, description, & item . auth ) ;
193
195
} else {
194
196
let name = match & item. name {
195
197
Some ( n) => n,
@@ -206,7 +208,11 @@ impl<'a> Transpiler<'a> {
206
208
items : & [ postman:: Items ] ,
207
209
name : & str ,
208
210
description : Option < String > ,
211
+ auth : & Option < postman:: Auth > ,
209
212
) {
213
+ let mut pushed_tag = false ;
214
+ let mut pushed_auth = false ;
215
+
210
216
if let Some ( t) = & mut state. oas . tags {
211
217
let mut tag = openapi3:: Tag {
212
218
name : name. to_string ( ) ,
@@ -223,9 +229,35 @@ impl<'a> Transpiler<'a> {
223
229
t. insert ( tag) ;
224
230
225
231
state. hierarchy . push ( name) ;
226
- self . transform ( state , items ) ;
227
- state . hierarchy . pop ( ) ;
232
+
233
+ pushed_tag = true ;
228
234
} ;
235
+
236
+ if let Some ( auth) = auth {
237
+ let security = self . transform_security ( state, auth) ;
238
+ if let Some ( pair) = security {
239
+ if let Some ( ( name, scopes) ) = pair {
240
+ state. auth_stack . push ( SecurityRequirement {
241
+ requirement : Some ( BTreeMap :: from ( [ ( name, scopes) ] ) ) ,
242
+ } ) ;
243
+ } else {
244
+ state
245
+ . auth_stack
246
+ . push ( SecurityRequirement { requirement : None } ) ;
247
+ }
248
+ pushed_auth = true ;
249
+ }
250
+ }
251
+
252
+ self . transform ( state, items) ;
253
+
254
+ if pushed_tag {
255
+ state. hierarchy . pop ( ) ;
256
+ }
257
+
258
+ if pushed_auth {
259
+ state. auth_stack . pop ( ) ;
260
+ }
229
261
}
230
262
231
263
fn transform_request ( & self , state : & mut TranspileState , item : & postman:: Items , name : & str ) {
@@ -241,7 +273,26 @@ impl<'a> Transpiler<'a> {
241
273
_ => & root_path,
242
274
} ;
243
275
244
- self . transform_paths ( state, item, request, name, u, paths)
276
+ let security_requirement = if let Some ( auth) = & request. auth {
277
+ let security = self . transform_security ( state, auth) ;
278
+ if let Some ( pair) = security {
279
+ if let Some ( ( name, scopes) ) = pair {
280
+ Some ( vec ! [ SecurityRequirement {
281
+ requirement: Some ( BTreeMap :: from( [ ( name, scopes) ] ) ) ,
282
+ } ] )
283
+ } else {
284
+ Some ( vec ! [ SecurityRequirement { requirement: None } ] )
285
+ }
286
+ } else {
287
+ None
288
+ }
289
+ } else if !state. auth_stack . is_empty ( ) {
290
+ Some ( vec ! [ state. auth_stack. last( ) . unwrap( ) . clone( ) ] )
291
+ } else {
292
+ None
293
+ } ;
294
+
295
+ self . transform_paths ( state, item, request, name, u, paths, security_requirement)
245
296
}
246
297
}
247
298
}
@@ -271,6 +322,7 @@ impl<'a> Transpiler<'a> {
271
322
}
272
323
}
273
324
325
+ #[ allow( clippy:: too_many_arguments) ]
274
326
fn transform_paths (
275
327
& self ,
276
328
state : & mut TranspileState ,
@@ -279,6 +331,7 @@ impl<'a> Transpiler<'a> {
279
331
request_name : & str ,
280
332
url : & postman:: UrlClass ,
281
333
paths : & [ postman:: PathElement ] ,
334
+ security_requirement : Option < Vec < SecurityRequirement > > ,
282
335
) {
283
336
let resolved_segments = paths
284
337
. iter ( )
@@ -334,6 +387,18 @@ impl<'a> Transpiler<'a> {
334
387
}
335
388
let op = op_ref. as_mut ( ) . unwrap ( ) ;
336
389
390
+ if let Some ( security_requirement) = security_requirement {
391
+ if let Some ( security) = & mut op. security {
392
+ for sr in security_requirement {
393
+ if !security. contains ( & sr) {
394
+ security. push ( sr) ;
395
+ }
396
+ }
397
+ } else {
398
+ op. security = Some ( security_requirement) ;
399
+ }
400
+ }
401
+
337
402
path. parameters = self . generate_path_parameters ( & resolved_segments, & url. variable ) ;
338
403
339
404
if !is_merge {
0 commit comments