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')