@@ -23,6 +23,7 @@ function prepareDefaultOptions (opts) {
23
23
const hiddenTag = opts . hiddenTag
24
24
const hideUntagged = opts . hideUntagged
25
25
const extensions = [ ]
26
+ const convertConstToEnum = opts . convertConstToEnum
26
27
27
28
for ( const [ key , value ] of Object . entries ( opts . openapi ) ) {
28
29
if ( key . startsWith ( 'x-' ) ) {
@@ -43,7 +44,8 @@ function prepareDefaultOptions (opts) {
43
44
transformObject,
44
45
hiddenTag,
45
46
extensions,
46
- hideUntagged
47
+ hideUntagged,
48
+ convertConstToEnum
47
49
}
48
50
}
49
51
@@ -130,8 +132,8 @@ function convertExamplesArrayToObject (examples) {
130
132
131
133
// For supported keys read:
132
134
// https://swagger.io/docs/specification/describing-parameters/
133
- function plainJsonObjectToOpenapi3 ( container , jsonSchema , externalSchemas , securityIgnores = [ ] ) {
134
- const obj = convertJsonSchemaToOpenapi3 ( resolveLocalRef ( jsonSchema , externalSchemas ) )
135
+ function plainJsonObjectToOpenapi3 ( opts , container , jsonSchema , externalSchemas , securityIgnores = [ ] ) {
136
+ const obj = convertJsonSchemaToOpenapi3 ( opts , resolveLocalRef ( jsonSchema , externalSchemas ) )
135
137
let toOpenapiProp
136
138
switch ( container ) {
137
139
case 'cookie' :
@@ -262,8 +264,8 @@ function schemaToMediaRecursive (schema) {
262
264
return media
263
265
}
264
266
265
- function resolveBodyParams ( body , schema , consumes , ref ) {
266
- const resolved = convertJsonSchemaToOpenapi3 ( ref . resolve ( schema ) )
267
+ function resolveBodyParams ( opts , body , schema , consumes , ref ) {
268
+ const resolved = convertJsonSchemaToOpenapi3 ( opts , ref . resolve ( schema ) )
267
269
268
270
if ( resolved . content ?. [ Object . keys ( resolved . content ) [ 0 ] ] . schema ) {
269
271
for ( const contentType in schema . content ) {
@@ -289,9 +291,9 @@ function resolveBodyParams (body, schema, consumes, ref) {
289
291
}
290
292
}
291
293
292
- function resolveCommonParams ( container , parameters , schema , ref , sharedSchemas , securityIgnores ) {
294
+ function resolveCommonParams ( opts , container , parameters , schema , ref , sharedSchemas , securityIgnores ) {
293
295
const schemasPath = '#/components/schemas/'
294
- let resolved = convertJsonSchemaToOpenapi3 ( ref . resolve ( schema ) )
296
+ let resolved = convertJsonSchemaToOpenapi3 ( opts , ref . resolve ( schema ) )
295
297
296
298
// if the resolved definition is in global schema
297
299
if ( resolved . $ref ?. startsWith ( schemasPath ) ) {
@@ -300,7 +302,7 @@ function resolveCommonParams (container, parameters, schema, ref, sharedSchemas,
300
302
resolved = pathParts . reduce ( ( resolved , pathPart ) => resolved [ pathPart ] , ref . definitions ( ) . definitions )
301
303
}
302
304
303
- const arr = plainJsonObjectToOpenapi3 ( container , resolved , { ...sharedSchemas , ...ref . definitions ( ) . definitions } , securityIgnores )
305
+ const arr = plainJsonObjectToOpenapi3 ( opts , container , resolved , { ...sharedSchemas , ...ref . definitions ( ) . definitions } , securityIgnores )
304
306
arr . forEach ( swaggerSchema => parameters . push ( swaggerSchema ) )
305
307
}
306
308
@@ -310,7 +312,7 @@ function findReferenceDescription (rawSchema, ref) {
310
312
}
311
313
312
314
// https://swagger.io/docs/specification/describing-responses/
313
- function resolveResponse ( fastifyResponseJson , produces , ref ) {
315
+ function resolveResponse ( opts , fastifyResponseJson , produces , ref ) {
314
316
// if the user does not provided an out schema
315
317
if ( ! fastifyResponseJson ) {
316
318
return { 200 : { description : 'Default Response' } }
@@ -322,7 +324,7 @@ function resolveResponse (fastifyResponseJson, produces, ref) {
322
324
323
325
statusCodes . forEach ( statusCode => {
324
326
const rawJsonSchema = fastifyResponseJson [ statusCode ]
325
- const resolved = convertJsonSchemaToOpenapi3 ( ref . resolve ( rawJsonSchema ) )
327
+ const resolved = convertJsonSchemaToOpenapi3 ( opts , ref . resolve ( rawJsonSchema ) )
326
328
327
329
/**
328
330
* 2xx require to be all upper-case
@@ -388,7 +390,7 @@ function resolveResponse (fastifyResponseJson, produces, ref) {
388
390
return responsesContainer
389
391
}
390
392
391
- function resolveCallbacks ( schema , ref ) {
393
+ function resolveCallbacks ( opts , schema , ref ) {
392
394
const callbacksContainer = { }
393
395
394
396
// Iterate over each callback event
@@ -422,13 +424,14 @@ function resolveCallbacks (schema, ref) {
422
424
423
425
if ( httpMethodSchema . requestBody ) {
424
426
httpMethodContainer . requestBody = convertJsonSchemaToOpenapi3 (
427
+ opts ,
425
428
ref . resolve ( httpMethodSchema . requestBody )
426
429
)
427
430
}
428
431
429
432
// If a response is not provided, set a 2XX default response
430
433
httpMethodContainer . responses = httpMethodSchema . responses
431
- ? convertJsonSchemaToOpenapi3 ( ref . resolve ( httpMethodSchema . responses ) )
434
+ ? convertJsonSchemaToOpenapi3 ( opts , ref . resolve ( httpMethodSchema . responses ) )
432
435
: { '2XX' : { description : 'Default Response' } }
433
436
434
437
// Set the schema at the appropriate location in the response object
@@ -440,7 +443,7 @@ function resolveCallbacks (schema, ref) {
440
443
return callbacksContainer
441
444
}
442
445
443
- function prepareOpenapiMethod ( schema , ref , openapiObject , url ) {
446
+ function prepareOpenapiMethod ( opts , schema , ref , openapiObject , url ) {
444
447
const openapiMethod = { }
445
448
const parameters = [ ]
446
449
@@ -470,21 +473,21 @@ function prepareOpenapiMethod (schema, ref, openapiObject, url) {
470
473
if ( schema . tags ) openapiMethod . tags = schema . tags
471
474
if ( schema . description ) openapiMethod . description = schema . description
472
475
if ( schema . externalDocs ) openapiMethod . externalDocs = schema . externalDocs
473
- if ( schema . querystring ) resolveCommonParams ( 'query' , parameters , schema . querystring , ref , openapiObject . definitions , securityIgnores . query )
476
+ if ( schema . querystring ) resolveCommonParams ( opts , 'query' , parameters , schema . querystring , ref , openapiObject . definitions , securityIgnores . query )
474
477
if ( schema . body ) {
475
478
openapiMethod . requestBody = { content : { } }
476
- resolveBodyParams ( openapiMethod . requestBody , schema . body , schema . consumes , ref )
479
+ resolveBodyParams ( opts , openapiMethod . requestBody , schema . body , schema . consumes , ref )
477
480
}
478
- if ( schema . params ) resolveCommonParams ( 'path' , parameters , schema . params , ref , openapiObject . definitions )
479
- if ( schema . headers ) resolveCommonParams ( 'header' , parameters , schema . headers , ref , openapiObject . definitions , securityIgnores . header )
481
+ if ( schema . params ) resolveCommonParams ( opts , 'path' , parameters , schema . params , ref , openapiObject . definitions )
482
+ if ( schema . headers ) resolveCommonParams ( opts , 'header' , parameters , schema . headers , ref , openapiObject . definitions , securityIgnores . header )
480
483
// TODO: need to documentation, we treat it same as the querystring
481
484
// fastify do not support cookies schema in first place
482
- if ( schema . cookies ) resolveCommonParams ( 'cookie' , parameters , schema . cookies , ref , openapiObject . definitions , securityIgnores . cookie )
485
+ if ( schema . cookies ) resolveCommonParams ( opts , 'cookie' , parameters , schema . cookies , ref , openapiObject . definitions , securityIgnores . cookie )
483
486
if ( parameters . length > 0 ) openapiMethod . parameters = parameters
484
487
if ( schema . deprecated ) openapiMethod . deprecated = schema . deprecated
485
488
if ( schema . security ) openapiMethod . security = schema . security
486
489
if ( schema . servers ) openapiMethod . servers = schema . servers
487
- if ( schema . callbacks ) openapiMethod . callbacks = resolveCallbacks ( schema . callbacks , ref )
490
+ if ( schema . callbacks ) openapiMethod . callbacks = resolveCallbacks ( opts , schema . callbacks , ref )
488
491
for ( const key of Object . keys ( schema ) ) {
489
492
if ( key . startsWith ( 'x-' ) ) {
490
493
openapiMethod [ key ] = schema [ key ]
@@ -495,22 +498,22 @@ function prepareOpenapiMethod (schema, ref, openapiObject, url) {
495
498
// If there is no schema or schema.params, we need to generate them
496
499
if ( ( ! schema || ! schema . params ) && hasParams ( url ) ) {
497
500
const schemaGenerated = generateParamsSchema ( url )
498
- resolveCommonParams ( 'path' , parameters , schemaGenerated . params , ref , openapiObject . definitions )
501
+ resolveCommonParams ( opts , 'path' , parameters , schemaGenerated . params , ref , openapiObject . definitions )
499
502
openapiMethod . parameters = parameters
500
503
}
501
504
502
- openapiMethod . responses = resolveResponse ( schema ? schema . response : null , schema ? schema . produces : null , ref )
505
+ openapiMethod . responses = resolveResponse ( opts , schema ? schema . response : null , schema ? schema . produces : null , ref )
503
506
504
507
return openapiMethod
505
508
}
506
509
507
- function convertJsonSchemaToOpenapi3 ( jsonSchema ) {
510
+ function convertJsonSchemaToOpenapi3 ( opts , jsonSchema ) {
508
511
if ( typeof jsonSchema !== 'object' || jsonSchema === null ) {
509
512
return jsonSchema
510
513
}
511
514
512
515
if ( Array . isArray ( jsonSchema ) ) {
513
- return jsonSchema . map ( convertJsonSchemaToOpenapi3 )
516
+ return jsonSchema . map ( ( s ) => convertJsonSchemaToOpenapi3 ( opts , s ) )
514
517
}
515
518
516
519
const openapiSchema = { ...jsonSchema }
@@ -529,7 +532,7 @@ function convertJsonSchemaToOpenapi3 (jsonSchema) {
529
532
continue
530
533
}
531
534
532
- if ( key === 'const' ) {
535
+ if ( opts . convertConstToEnum && key === 'const' ) {
533
536
// OAS 3.1 supports `const` but it is not supported by `swagger-ui`
534
537
// https://swagger.io/docs/specification/data-models/keywords/
535
538
// TODO: check if enum property already exists
@@ -545,7 +548,7 @@ function convertJsonSchemaToOpenapi3 (jsonSchema) {
545
548
// TODO: patternProperties actually allowed in the openapi schema, but should
546
549
// always start with "x-" prefix
547
550
const propertyJsonSchema = Object . values ( openapiSchema . patternProperties ) [ 0 ]
548
- const propertyOpenapiSchema = convertJsonSchemaToOpenapi3 ( propertyJsonSchema )
551
+ const propertyOpenapiSchema = convertJsonSchemaToOpenapi3 ( opts , propertyJsonSchema )
549
552
openapiSchema . additionalProperties = propertyOpenapiSchema
550
553
delete openapiSchema . patternProperties
551
554
continue
@@ -555,26 +558,26 @@ function convertJsonSchemaToOpenapi3 (jsonSchema) {
555
558
openapiSchema [ key ] = { }
556
559
for ( const propertyName of Object . keys ( value ) ) {
557
560
const propertyJsonSchema = value [ propertyName ]
558
- const propertyOpenapiSchema = convertJsonSchemaToOpenapi3 ( propertyJsonSchema )
561
+ const propertyOpenapiSchema = convertJsonSchemaToOpenapi3 ( opts , propertyJsonSchema )
559
562
openapiSchema [ key ] [ propertyName ] = propertyOpenapiSchema
560
563
}
561
564
continue
562
565
}
563
566
564
- openapiSchema [ key ] = convertJsonSchemaToOpenapi3 ( value )
567
+ openapiSchema [ key ] = convertJsonSchemaToOpenapi3 ( opts , value )
565
568
}
566
569
567
570
return openapiSchema
568
571
}
569
572
570
- function prepareOpenapiSchemas ( jsonSchemas , ref ) {
573
+ function prepareOpenapiSchemas ( opts , jsonSchemas , ref ) {
571
574
const openapiSchemas = { }
572
575
573
576
for ( const schemaName of Object . keys ( jsonSchemas ) ) {
574
577
const jsonSchema = { ...jsonSchemas [ schemaName ] }
575
578
576
579
const resolvedJsonSchema = ref . resolve ( jsonSchema , { externalSchemas : [ jsonSchemas ] } )
577
- const openapiSchema = convertJsonSchemaToOpenapi3 ( resolvedJsonSchema )
580
+ const openapiSchema = convertJsonSchemaToOpenapi3 ( opts , resolvedJsonSchema )
578
581
resolveSchemaExamplesRecursive ( openapiSchema )
579
582
580
583
openapiSchemas [ schemaName ] = openapiSchema
0 commit comments