From c4dd960721454e77a59b906ffe108a338ec3e3ef Mon Sep 17 00:00:00 2001 From: Brice RUZAND Date: Mon, 20 Sep 2021 15:04:02 +0200 Subject: [PATCH] #462 Allow nested $ref to resolve for OpenAPI implementation --- lib/spec/openapi/index.js | 12 +++++++++++- test/spec/openapi/refs.js | 4 +--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/spec/openapi/index.js b/lib/spec/openapi/index.js index 064b80cf..872c8111 100644 --- a/lib/spec/openapi/index.js +++ b/lib/spec/openapi/index.js @@ -23,7 +23,17 @@ module.exports = function (opts, cache, routes, Ref, done) { openapiObject.components.schemas = prepareOpenapiSchemas({ ...openapiObject.components.schemas, ...(ref.definitions().definitions) - },ref) + }, ref) + + // Swagger doesn't accept $id on /definitions schemas. + // The $ids are needed by Ref() to check the URI so we need + // to remove them at the end of the process + // definitions are added by resolve but they are replace by components.schemas + Object.values(openapiObject.components.schemas) + .forEach((_) => { + delete _.$id + delete _.definitions + }) for (const route of routes) { const schema = defOpts.transform diff --git a/test/spec/openapi/refs.js b/test/spec/openapi/refs.js index 33abf4ea..fbae7b40 100644 --- a/test/spec/openapi/refs.js +++ b/test/spec/openapi/refs.js @@ -53,11 +53,9 @@ test('support nested $ref schema', (t) => { t.equal(typeof openapiObject, 'object') t.match(Object.keys(openapiObject.components.schemas), ['def-0', 'def-1']) - // OrderItem ref is not prefixed by '#/components/schemas/' + // OrderItem ref must be prefixed by '#/components/schemas/' t.equal(openapiObject.components.schemas['def-1'].properties.products.items.$ref, '#/components/schemas/def-0') - // open api validation failed - // ENOENT: no such file or directory ${cwd}/OrderItem Swagger.validate(openapiObject) .then(function (api) { t.pass('valid swagger object')