Skip to content
This repository has been archived by the owner on Jul 2, 2019. It is now read-only.

Commit

Permalink
fix(json-schema): Count for arrays inside a schema reference checker
Browse files Browse the repository at this point in the history
The schema could contain anyOf, allOf, items etc that are arrays so we
need to traverse those too.
  • Loading branch information
kylef committed Sep 4, 2018
1 parent 67066a3 commit 605e9d0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ function checkSchemaHasReferences(schema) {
return true;
}

let hasRef = false;

Object.values(schema).forEach((value) => {
if (_.isObject(value) && checkSchemaHasReferences(value)) {
hasRef = true;
return Object.values(schema).some((value) => {
if (_.isArray(value)) {
return value.some(checkSchemaHasReferences);
} else if (_.isObject(value)) {
return checkSchemaHasReferences(value);
}
});

return hasRef;
return false;
});
}

/** Convert Swagger schema to JSON Schema
Expand Down
41 changes: 41 additions & 0 deletions test/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,47 @@ describe('Swagger Schema to JSON Schema', () => {
});
});

it('does not dererferences root when references found inside schema with items', () => {
const root = {
definitions: {
Node: {
type: 'object',
properties: {
children: {
type: 'array',
items: [
{ $ref: '#/definitions/Node' },
],
},
},
},
},
};

const schema = convertSchema({
$ref: '#/definitions/Node',
}, root);

expect(schema).to.deep.equal({
allOf: [
{ $ref: '#/definitions/Node' },
],
definitions: {
Node: {
type: 'object',
properties: {
children: {
type: 'array',
items: [
{ $ref: '#/definitions/Node' },
],
},
},
},
},
});
});

it('copies references to schema', () => {
const root = {
definitions: {
Expand Down

0 comments on commit 605e9d0

Please sign in to comment.