@@ -11,7 +11,7 @@ import { generateMediaTypeExamples, generateSchemaExample } from './generateSche
1111import { stringifyOpenAPI } from './stringifyOpenAPI' ;
1212import type { OpenAPIOperationData } from './types' ;
1313import { getDefaultServerURL } from './util/server' ;
14- import { checkIsReference } from './utils' ;
14+ import { checkIsReference , extractOperationSecurityInfo } from './utils' ;
1515
1616const CUSTOM_CODE_SAMPLES_KEYS = [ 'x-custom-examples' , 'x-code-samples' , 'x-codeSamples' ] as const ;
1717
@@ -106,7 +106,10 @@ function generateCodeSamples(props: {
106106 ( searchParams . size ? `?${ searchParams . toString ( ) } ` : '' ) ;
107107
108108 const genericHeaders = {
109- ...getSecurityHeaders ( data . securities ) ,
109+ ...getSecurityHeaders ( {
110+ securityRequirement : data . operation . security ,
111+ securities : data . securities ,
112+ } ) ,
110113 ...headersObject ,
111114 } ;
112115
@@ -278,51 +281,66 @@ function getCustomCodeSamples(props: {
278281 return customCodeSamples ;
279282}
280283
281- function getSecurityHeaders ( securities : OpenAPIOperationData [ 'securities' ] ) : {
284+ function getSecurityHeaders ( args : {
285+ securityRequirement : OpenAPIV3 . OperationObject [ 'security' ] ;
286+ securities : OpenAPIOperationData [ 'securities' ] ;
287+ } ) : {
282288 [ key : string ] : string ;
283289} {
284- const security = securities [ 0 ] ;
290+ const { securityRequirement, securities } = args ;
291+ const operationSecurityInfo = extractOperationSecurityInfo ( { securityRequirement, securities } ) ;
285292
286- if ( ! security ) {
293+ if ( operationSecurityInfo . length === 0 ) {
287294 return { } ;
288295 }
289296
290- switch ( security [ 1 ] . type ) {
291- case 'http' : {
292- let scheme = security [ 1 ] . scheme ;
293- let format = security [ 1 ] . bearerFormat ?? 'YOUR_SECRET_TOKEN' ;
294-
295- if ( scheme ?. includes ( 'bearer' ) ) {
296- scheme = 'Bearer' ;
297- } else if ( scheme ?. includes ( 'basic' ) ) {
298- scheme = 'Basic' ;
299- format = 'username:password' ;
300- } else if ( scheme ?. includes ( 'token' ) ) {
301- scheme = 'Token' ;
302- }
297+ const selectedSecurity = operationSecurityInfo . at ( 0 ) ;
303298
304- return {
305- Authorization : `${ scheme } ${ format } ` ,
306- } ;
307- }
308- case 'apiKey' : {
309- if ( security [ 1 ] . in !== 'header' ) return { } ;
299+ if ( ! selectedSecurity ) {
300+ return { } ;
301+ }
310302
311- const name = security [ 1 ] . name ?? 'Authorization' ;
303+ const headers : { [ key : string ] : string } = { } ;
304+
305+ for ( const security of selectedSecurity . schemes ) {
306+ switch ( security . type ) {
307+ case 'http' : {
308+ let scheme = security . scheme ;
309+ let format = security . bearerFormat ?? 'YOUR_SECRET_TOKEN' ;
310+
311+ if ( scheme ?. includes ( 'bearer' ) ) {
312+ scheme = 'Bearer' ;
313+ } else if ( scheme ?. includes ( 'basic' ) ) {
314+ scheme = 'Basic' ;
315+ format = 'username:password' ;
316+ } else if ( scheme ?. includes ( 'token' ) ) {
317+ scheme = 'Token' ;
318+ }
319+
320+ headers . Authorization = `${ scheme } ${ format } ` ;
321+ break ;
322+ }
323+ case 'apiKey' : {
324+ if ( security . in !== 'header' ) {
325+ break ;
326+ }
312327
313- return {
314- [ name ] : 'YOUR_API_KEY' ,
315- } ;
316- }
317- case 'oauth2' : {
318- return {
319- Authorization : 'Bearer YOUR_OAUTH2_TOKEN' ,
320- } ;
321- }
322- default : {
323- return { } ;
328+ const name = security . name ?? 'Authorization' ;
329+ headers [ name ] = 'YOUR_API_KEY' ;
330+
331+ break ;
332+ }
333+ case 'oauth2' : {
334+ headers . Authorization = 'Bearer YOUR_OAUTH2_TOKEN' ;
335+ break ;
336+ }
337+ default : {
338+ break ;
339+ }
324340 }
325341 }
342+
343+ return headers ;
326344}
327345
328346function validateHttpMethod ( method : string ) : method is OpenAPIV3 . HttpMethods {
0 commit comments