Skip to content

Commit 56dbc02

Browse files
authored
fix: remove defs when ref already defined in schema (#888)
* Fix: Remove defs when ref already defined - Remove $defs from schema keys if $ref key already exists when converting json schema into openapi 3. This fixes issue in openapi generation when using TypeBox modules to define schema. - Add test case for refs. * Remove sibling keys if $refs exists - Remove sibling keys if $refs exits on openapi 3 json schema.
1 parent 041a3f3 commit 56dbc02

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

lib/spec/openapi/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,13 @@ function convertJsonSchemaToOpenapi3 (jsonSchema) {
515515

516516
const openapiSchema = { ...jsonSchema }
517517

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+
518525
for (const key of Object.keys(openapiSchema)) {
519526
const value = openapiSchema[key]
520527

test/spec/openapi/refs.test.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,135 @@ test('support $ref in callbacks', async (t) => {
430430

431431
await Swagger.validate(openapiObject)
432432
})
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+
})

0 commit comments

Comments
 (0)