diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js index 509e1561..f16e83ef 100644 --- a/lib/spec/openapi/utils.js +++ b/lib/spec/openapi/utils.js @@ -425,7 +425,7 @@ function prepareOpenapiSchemas (schemas, ref) { // swagger doesn't accept $id on components schemas // $ids are needed by ref.resolve to check the URI // definitions are added by resolve, but they are not needed, as we resolve - // the $ref to already existing schemas in components.schemas using method below, not the definition ones + // the $ref to already existing schemas in components.schemas using method below // therefore, we delete both $id and definitions at the end of the process delete resolvedRef.$id delete resolvedRef.definitions diff --git a/test/spec/openapi/refs.js b/test/spec/openapi/refs.js index 0481385f..4be5336e 100644 --- a/test/spec/openapi/refs.js +++ b/test/spec/openapi/refs.js @@ -290,6 +290,9 @@ test('support schema with definitions keyword and $ref inside', async (t) => { fastify.register(async (instance) => { instance.addSchema({ $id: 'NestedSchema', + properties: { + id: { type: 'string' }, + }, definitions: { SchemaA: { type: 'object', @@ -312,9 +315,11 @@ test('support schema with definitions keyword and $ref inside', async (t) => { const openapiObject = fastify.swagger() t.equal(typeof openapiObject, 'object') + + // definitions are getting merged to properties t.match(Object.keys(openapiObject.components.schemas), ['NestedSchema']) t.match(Object.keys(openapiObject.components.schemas.NestedSchema), ['properties']) - t.match(Object.keys(openapiObject.components.schemas.NestedSchema.properties), ['SchemaA', 'SchemaB']) + t.match(Object.keys(openapiObject.components.schemas.NestedSchema.properties), ['id', 'SchemaA', 'SchemaB']) // ref must be prefixed by '#/components/schemas/' t.equal(openapiObject.components.schemas.NestedSchema.properties.SchemaB.properties.example.$ref, '#/components/schemas/NestedSchema/properties/SchemaA')