Skip to content

Commit

Permalink
fix: support true/false JSON Schemas (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Jun 16, 2021
1 parent 269206c commit 5cc631b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/models/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ParserError = require('../errors/parser-error');
*/
class Base {
constructor (json) {
if (!json) throw new ParserError(`Invalid JSON to instantiate the ${this.constructor.name} object.`);
if (json === undefined || json === null) throw new ParserError(`Invalid JSON to instantiate the ${this.constructor.name} object.`);
this._json = json;
}

Expand Down
7 changes: 7 additions & 0 deletions lib/models/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,13 @@ class Schema extends Base {
return this._json.examples;
}

/**
* @returns {boolean}
*/
isBooleanSchema() {
return typeof this._json === 'boolean';
}

/**
* @returns {boolean}
*/
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
},
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^9.0.6",
"@asyncapi/specs": "^2.7.7",
"@asyncapi/specs": "^2.7.8",
"@fmvilas/pseudo-yaml-ast": "^0.3.1",
"ajv": "^6.10.1",
"js-yaml": "^3.13.1",
Expand Down
71 changes: 59 additions & 12 deletions test/asyncapiSchemaFormatParser_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,6 @@ describe('asyncapiSchemaFormatParser', function() {
endOffset: offset(297, 15)
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be object',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be boolean',
location: {
Expand Down Expand Up @@ -110,6 +98,65 @@ describe('asyncapiSchemaFormatParser', function() {
expect(async () => await parser.parse(parsedInput)).to.not.throw();
});

it('should handle true/false JSON Schemas', async function() {
const inputSpec = {
asyncapi: '2.0.0',
info: {
title: 'Example Spec',
version: '1.0.0',
},
channels: {
testChannel: {
publish: {
message: {
payload: {
type: 'object',
properties: {
trueSchema: true,
falseSchema: false,
normalSchema: {
type: 'string',
}
},
}
}
},
subscribe: {
message: {
headers: true,
payload: false,
}
},
},
testChanne2: {
publish: {
message: {
payload: true,
}
}
}
},
components: {
schemas: {
testSchema: {
type: 'object',
properties: {
trueSchema: true,
falseSchema: false,
normalSchema: {
type: 'string',
}
},
},
anySchema: true,
cannotBeDefined: false,
}
}
};

expect(async () => await parser.parse(inputSpec)).to.not.throw();
});

it('should deep clone schema into x-parser-original-payload', async function() {
const asyncapi = fs.readFileSync(path.resolve(__dirname, './good/asyncapi-complex-schema.yml'), 'utf8');
const expectedOriginalPayload = {
Expand Down
22 changes: 22 additions & 0 deletions test/models/schema_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,28 @@ describe('Schema', function() {
});
});

describe('#isBooleanSchema()', function() {
it('should return a true when schema is true', function() {
const d = new Schema(true);
expect(d.isBooleanSchema()).to.be.equal(true);
});

it('_json property should equal to true when schema is true', function() {
const d = new Schema(true);
expect(d.json()).to.be.equal(true);
});

it('should return a true when schema is false', function() {
const d = new Schema(false);
expect(d.isBooleanSchema()).to.be.equal(true);
});

it('_json property should equal to false when schema is false', function() {
const d = new Schema(false);
expect(d.json()).to.be.equal(false);
});
});

describe('#isCircular()', function() {
it('should return a boolean', function() {
const doc = { 'x-parser-circular': true};
Expand Down

0 comments on commit 5cc631b

Please sign in to comment.