File tree Expand file tree Collapse file tree 2 files changed +139
-0
lines changed Expand file tree Collapse file tree 2 files changed +139
-0
lines changed Original file line number Diff line number Diff line change @@ -515,6 +515,13 @@ function convertJsonSchemaToOpenapi3 (jsonSchema) {
515
515
516
516
const openapiSchema = { ...jsonSchema }
517
517
518
+ if ( Object . hasOwn ( openapiSchema , '$ref' ) && Object . keys ( openapiSchema ) . length !== 1 ) {
519
+ for ( const key of Object . keys ( openapiSchema ) . filter ( k => k !== '$ref' ) ) {
520
+ delete openapiSchema [ key ]
521
+ continue
522
+ }
523
+ }
524
+
518
525
for ( const key of Object . keys ( openapiSchema ) ) {
519
526
const value = openapiSchema [ key ]
520
527
Original file line number Diff line number Diff line change @@ -430,3 +430,135 @@ test('support $ref in callbacks', async (t) => {
430
430
431
431
await Swagger . validate ( openapiObject )
432
432
} )
433
+
434
+ test ( 'should return only ref if defs and ref is defined' , async ( t ) => {
435
+ const fastify = Fastify ( )
436
+ await fastify . register ( fastifySwagger , { openapi : { openapi : '3.1.0' } } )
437
+
438
+ fastify . addSchema ( {
439
+ $id : 'sharedSchema' ,
440
+ humanModule : {
441
+ $defs : {
442
+ AddressSchema : {
443
+ type : 'object' ,
444
+ properties : {
445
+ street : {
446
+ type : 'string' ,
447
+ } ,
448
+ streetNumber : {
449
+ type : 'number' ,
450
+ } ,
451
+ } ,
452
+ required : [
453
+ 'street' ,
454
+ 'streetNumber' ,
455
+ ] ,
456
+ $id : 'AddressSchema' ,
457
+ } ,
458
+ PersonSchema : {
459
+ type : 'object' ,
460
+ properties : {
461
+ name : {
462
+ type : 'string' ,
463
+ } ,
464
+ homeAddress : {
465
+ $ref : 'AddressSchema' ,
466
+ } ,
467
+ workAddress : {
468
+ $ref : 'AddressSchema' ,
469
+ } ,
470
+ } ,
471
+ required : [
472
+ 'name' ,
473
+ 'homeAddress' ,
474
+ 'workAddress' ,
475
+ ] ,
476
+ $id : 'PersonSchema' ,
477
+ } ,
478
+ PostRequestSchema : {
479
+ type : 'object' ,
480
+ properties : {
481
+ person : {
482
+ $ref : 'PersonSchema' ,
483
+ } ,
484
+ } ,
485
+ required : [
486
+ 'person' ,
487
+ ] ,
488
+ $id : 'PostRequestSchema' ,
489
+ } ,
490
+ } ,
491
+ } ,
492
+ } )
493
+ fastify . get ( '/person' , {
494
+ schema : {
495
+ response : {
496
+ 200 :
497
+ {
498
+ $defs : {
499
+ AddressSchema : {
500
+ type : 'object' ,
501
+ properties : {
502
+ street : {
503
+ type : 'string' ,
504
+ } ,
505
+ streetNumber : {
506
+ type : 'number' ,
507
+ } ,
508
+ } ,
509
+ required : [
510
+ 'street' ,
511
+ 'streetNumber' ,
512
+ ] ,
513
+ $id : 'AddressSchema' ,
514
+ } ,
515
+ PersonSchema : {
516
+ type : 'object' ,
517
+ properties : {
518
+ name : {
519
+ type : 'string' ,
520
+ } ,
521
+ homeAddress : {
522
+ $ref : 'AddressSchema' ,
523
+ } ,
524
+ workAddress : {
525
+ $ref : 'AddressSchema' ,
526
+ } ,
527
+ } ,
528
+ required : [
529
+ 'name' ,
530
+ 'homeAddress' ,
531
+ 'workAddress' ,
532
+ ] ,
533
+ $id : 'PersonSchema' ,
534
+ } ,
535
+ PostRequestSchema : {
536
+ type : 'object' ,
537
+ properties : {
538
+ person : {
539
+ $ref : 'PersonSchema' ,
540
+ } ,
541
+ } ,
542
+ required : [
543
+ 'person' ,
544
+ ] ,
545
+ $id : 'PostRequestSchema' ,
546
+ } ,
547
+ } ,
548
+ $ref : 'PersonSchema' ,
549
+ }
550
+ }
551
+ } ,
552
+ } , async ( ) => ( { result : 'OK' } ) )
553
+
554
+ await fastify . ready ( )
555
+
556
+ const openapiObject = fastify . swagger ( )
557
+
558
+ t . assert . strictEqual ( typeof openapiObject , 'object' )
559
+
560
+ const expectedPathContent = { 'application/json' : { schema : { $ref : '#/components/schemas/def-2' } } }
561
+ t . assert . deepStrictEqual ( openapiObject . paths [ '/person' ] . get . responses [ 200 ] . content , expectedPathContent )
562
+
563
+ await Swagger . validate ( openapiObject )
564
+ } )
You can’t perform that action at this time.
0 commit comments