From c1a7b7274c2d55d893b331420978e9e118d651e2 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Mon, 5 Sep 2022 15:07:19 +0200 Subject: [PATCH] avoid crashing of resolveLocalRef (#662) --- lib/util/common.js | 11 ++++++---- test/spec/openapi/refs.js | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/lib/util/common.js b/lib/util/common.js index 189e5936..57135deb 100644 --- a/lib/util/common.js +++ b/lib/util/common.js @@ -187,10 +187,13 @@ function resolveLocalRef (jsonSchema, externalSchemas) { } // $ref is in the format: #/definitions// - const localRef = jsonSchema.$ref.split('/')[2] - if (externalSchemas[localRef]) return resolveLocalRef(externalSchemas[localRef], externalSchemas) - // $ref is in the format: #/components/schemas/ - return resolveLocalRef(externalSchemas[jsonSchema.$ref.split('/')[3]], externalSchemas) + if (jsonSchema.$ref) { + const localRef = jsonSchema.$ref.split('/')[2] + if (externalSchemas[localRef]) return resolveLocalRef(externalSchemas[localRef], externalSchemas) + // $ref is in the format: #/components/schemas/ + return resolveLocalRef(externalSchemas[jsonSchema.$ref.split('/')[3]], externalSchemas) + } + return jsonSchema } function readPackageJson () { diff --git a/test/spec/openapi/refs.js b/test/spec/openapi/refs.js index 08871cad..a7d758ef 100644 --- a/test/spec/openapi/refs.js +++ b/test/spec/openapi/refs.js @@ -238,3 +238,47 @@ test('support $ref schema in allOf in headers', async (t) => { t.equal(responseAfterSwagger.statusCode, 200) }) + +test('uses examples if has property required in body', async (t) => { + t.plan(3) + const fastify = Fastify() + + await fastify.register(fastifySwagger, openapiOption) + + fastify.get('/', { + schema: { + query: { + type: 'object', + oneOf: [ + { + properties: { + bar: { type: 'number' } + } + }, + { + properties: { + foo: { type: 'string' } + } + } + ] + }, + response: { + 200: { + type: 'object', + properties: { + result: { type: 'string' } + } + } + } + } + }, (req, reply) => ({ result: 'OK' })) + + await fastify.ready() + + const openapiObject = fastify.swagger() + const schema = openapiObject.paths['/'].get + + t.ok(schema) + t.ok(schema.parameters) + t.same(schema.parameters[0].in, 'query') +})