Skip to content

Commit

Permalink
fix: use proper json schema and tests to point to 2.1 (#327)
Browse files Browse the repository at this point in the history
Co-authored-by: Fran Méndez <[email protected]>
  • Loading branch information
derberg and fmvilas authored Jun 25, 2021
1 parent 4cf89d6 commit a17d6d1
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ npm install @asyncapi/parser
const parser = require('@asyncapi/parser');

const doc = await parser.parse(`
asyncapi: '2.0.0'
asyncapi: '2.1.0'
info:
title: Example
version: '0.1.0'
Expand Down
3 changes: 3 additions & 0 deletions lib/asyncapiSchemaFormatParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ async function parse({ message, originalAsyncAPIDocument, fileFormat, parsedAsyn
function getMimeTypes() {
return [
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi;version=2.1.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.1.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.1.0',
'application/schema;version=draft-07',
'application/schema+json;version=draft-07',
'application/schema+yaml;version=draft-07',
Expand Down
2 changes: 1 addition & 1 deletion lib/models/message-traitable.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MessageTraitable extends Base {
* @returns {string}
*/
schemaFormat() {
return 'application/schema+json;version=draft-07';
return this._json.schemaFormat;
}

/**
Expand Down
11 changes: 5 additions & 6 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ const $RefParser = require('@apidevtools/json-schema-ref-parser');
const mergePatch = require('tiny-merge-patch').apply;
const ParserError = require('./errors/parser-error');
const { validateChannels, validateServerVariables, validateOperationId, validateServerSecurity } = require('./customValidators.js');
const { toJS, findRefs, getLocationOf, improveAjvErrors } = require('./utils');
const { toJS, findRefs, getLocationOf, improveAjvErrors, getDefaultSchemaFormat } = require('./utils');
const AsyncAPIDocument = require('./models/asyncapi');

const DEFAULT_SCHEMA_FORMAT = 'application/vnd.aai.asyncapi;version=2.0.0';
const OPERATIONS = ['publish', 'subscribe'];
//the only security types that can have a non empty array in the server security item
const SPECIAL_SECURITY_TYPES = ['oauth2', 'openIdConnect'];
Expand Down Expand Up @@ -187,20 +186,20 @@ async function customDocumentOperations(parsedJSON, asyncapiYAMLorJSON, initialF
async function validateAndConvertMessage(msg, originalAsyncAPIDocument, fileFormat, parsedAsyncAPIDocument, pathToPayload) {
//check if the message has been parsed before
if (xParserMessageParsed in msg && msg[String(xParserMessageParsed)] === true) return;

const schemaFormat = msg.schemaFormat || DEFAULT_SCHEMA_FORMAT;
const defaultSchemaFormat = getDefaultSchemaFormat(parsedAsyncAPIDocument.asyncapi);
const schemaFormat = msg.schemaFormat || defaultSchemaFormat;

await PARSERS[String(schemaFormat)]({
schemaFormat,
message: msg,
defaultSchemaFormat: DEFAULT_SCHEMA_FORMAT,
defaultSchemaFormat,
originalAsyncAPIDocument,
parsedAsyncAPIDocument,
fileFormat,
pathToPayload
});

msg.schemaFormat = DEFAULT_SCHEMA_FORMAT;
msg.schemaFormat = defaultSchemaFormat;
msg[String(xParserMessageParsed)] = true;
}

Expand Down
11 changes: 11 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,14 @@ utils.setNotProvidedParams = (variables, val, key, notProvidedChannelParams, not
: missingChannelParams);
}
};

/**
* returns default schema format for a given asyncapi version
*
* @function getDefaultSchemaFormat
* @private
* @param {String} asyncapiVersion AsyncAPI spec version.
*/
utils.getDefaultSchemaFormat = (asyncapiVersion) => {
return `application/vnd.aai.asyncapi;version=${asyncapiVersion}`;
};
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.8.0-2021-06-release.3",
"@asyncapi/specs": "2.8.0-2021-06-release.5",
"@fmvilas/pseudo-yaml-ast": "^0.3.1",
"ajv": "^6.10.1",
"js-yaml": "^3.13.1",
Expand Down
2 changes: 1 addition & 1 deletion test/good/asyncapi-messages-example-headers.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
asyncapi: 2.0.0
asyncapi: 2.1.0
info:
title: My API
version: '1.0.0'
Expand Down
2 changes: 1 addition & 1 deletion test/good/asyncapi-messages-example-optional.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
asyncapi: 2.0.0
asyncapi: 2.1.0
info:
title: My API
version: '1.0.0'
Expand Down
2 changes: 1 addition & 1 deletion test/good/asyncapi-messages-example-payload.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
asyncapi: 2.0.0
asyncapi: 2.1.0
info:
title: My API
version: '1.0.0'
Expand Down
2 changes: 1 addition & 1 deletion test/good/asyncapi-messages-example.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
asyncapi: 2.0.0
asyncapi: 2.1.0
info:
title: My API
version: '1.0.0'
Expand Down
4 changes: 2 additions & 2 deletions test/models/message-trait_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai');
const js = { headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, correlationId: { test: true }, contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{name: 'test', summary: 'test summary', payload: {test: true}}], 'x-test': 'testing' };
const js = { schemaFormat: 'mySchema', headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, correlationId: { test: true }, contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{name: 'test', summary: 'test summary', payload: {test: true}}], 'x-test': 'testing' };

const MessageTrait = require('../../lib/models/message-trait');

Expand Down Expand Up @@ -50,7 +50,7 @@ describe('MessageTrait', function() {
describe('#schemaFormat()', function() {
it('should return a string', function() {
const d = new MessageTrait(js);
expect(d.schemaFormat()).to.equal('application/schema+json;version=draft-07');
expect(d.schemaFormat()).to.equal('mySchema');
});
});

Expand Down
4 changes: 2 additions & 2 deletions test/models/message-traitable_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai');
const js = { headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, correlationId: { test: true }, contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{name: 'test', summary: 'test summary', payload: {test: true}}], 'x-test': 'testing' };
const js = { schemaFormat: 'mySchema', headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, correlationId: { test: true }, contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{name: 'test', summary: 'test summary', payload: {test: true}}], 'x-test': 'testing' };

const MessageTraitable = require('../../lib/models/message-traitable');

Expand Down Expand Up @@ -50,7 +50,7 @@ describe('MessageTraitable', function() {
describe('#schemaFormat()', function() {
it('should return a string', function() {
const d = new MessageTraitable(js);
expect(d.schemaFormat()).to.equal('application/schema+json;version=draft-07');
expect(d.schemaFormat()).to.equal('mySchema');
});
});

Expand Down
6 changes: 3 additions & 3 deletions test/models/message_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai');
const js = { headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, payload: { test: true }, 'x-parser-original-payload': { testing: true }, correlationId: { test: true }, 'x-parser-original-schema-format': 'application/vnd.apache.avro;version=1.9.0', contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{name: 'test', summary: 'test summary', payload: {test: true}}], 'x-test': 'testing' };
const js = { schemaFormat: 'mySchema', headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, payload: { test: true }, 'x-parser-original-payload': { testing: true }, correlationId: { test: true }, 'x-parser-original-schema-format': 'application/vnd.apache.avro;version=1.9.0', contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{name: 'test', summary: 'test summary', payload: {test: true}}], 'x-test': 'testing' };

const Message = require('../../lib/models/message');

Expand All @@ -25,7 +25,7 @@ describe('Message', function() {
it('should return a string with the base64 representation of the object when x-parser-message-name extension and name are not available', function() {
const msg = { ...js, ...{ name: undefined } };
const d = new Message(msg);
expect(d.uid()).to.be.equal('eyJoZWFkZXJzIjp7InByb3BlcnRpZXMiOnsidGVzdDEiOnsidHlwZSI6InN0cmluZyJ9LCJ0ZXN0MiI6eyJ0eXBlIjoibnVtYmVyIn19fSwicGF5bG9hZCI6eyJ0ZXN0Ijp0cnVlfSwieC1wYXJzZXItb3JpZ2luYWwtcGF5bG9hZCI6eyJ0ZXN0aW5nIjp0cnVlfSwiY29ycmVsYXRpb25JZCI6eyJ0ZXN0Ijp0cnVlfSwieC1wYXJzZXItb3JpZ2luYWwtc2NoZW1hLWZvcm1hdCI6ImFwcGxpY2F0aW9uL3ZuZC5hcGFjaGUuYXZybzt2ZXJzaW9uPTEuOS4wIiwiY29udGVudFR5cGUiOiJhcHBsaWNhdGlvbi9qc29uIiwidGl0bGUiOiJUZXN0Iiwic3VtbWFyeSI6InRlc3QiLCJkZXNjcmlwdGlvbiI6InRlc3RpbmciLCJleHRlcm5hbERvY3MiOnsidGVzdCI6dHJ1ZX0sInRhZ3MiOlt7Im5hbWUiOiJ0YWcxIn1dLCJiaW5kaW5ncyI6eyJhbXFwIjp7InRlc3QiOnRydWV9fSwiZXhhbXBsZXMiOlt7Im5hbWUiOiJ0ZXN0Iiwic3VtbWFyeSI6InRlc3Qgc3VtbWFyeSIsInBheWxvYWQiOnsidGVzdCI6dHJ1ZX19XSwieC10ZXN0IjoidGVzdGluZyJ9');
expect(d.uid()).to.be.equal('eyJzY2hlbWFGb3JtYXQiOiJteVNjaGVtYSIsImhlYWRlcnMiOnsicHJvcGVydGllcyI6eyJ0ZXN0MSI6eyJ0eXBlIjoic3RyaW5nIn0sInRlc3QyIjp7InR5cGUiOiJudW1iZXIifX19LCJwYXlsb2FkIjp7InRlc3QiOnRydWV9LCJ4LXBhcnNlci1vcmlnaW5hbC1wYXlsb2FkIjp7InRlc3RpbmciOnRydWV9LCJjb3JyZWxhdGlvbklkIjp7InRlc3QiOnRydWV9LCJ4LXBhcnNlci1vcmlnaW5hbC1zY2hlbWEtZm9ybWF0IjoiYXBwbGljYXRpb24vdm5kLmFwYWNoZS5hdnJvO3ZlcnNpb249MS45LjAiLCJjb250ZW50VHlwZSI6ImFwcGxpY2F0aW9uL2pzb24iLCJ0aXRsZSI6IlRlc3QiLCJzdW1tYXJ5IjoidGVzdCIsImRlc2NyaXB0aW9uIjoidGVzdGluZyIsImV4dGVybmFsRG9jcyI6eyJ0ZXN0Ijp0cnVlfSwidGFncyI6W3sibmFtZSI6InRhZzEifV0sImJpbmRpbmdzIjp7ImFtcXAiOnsidGVzdCI6dHJ1ZX19LCJleGFtcGxlcyI6W3sibmFtZSI6InRlc3QiLCJzdW1tYXJ5IjoidGVzdCBzdW1tYXJ5IiwicGF5bG9hZCI6eyJ0ZXN0Ijp0cnVlfX1dLCJ4LXRlc3QiOiJ0ZXN0aW5nIn0=');
});
});

Expand Down Expand Up @@ -70,7 +70,7 @@ describe('Message', function() {
describe('#schemaFormat()', function() {
it('should return a string', function() {
const d = new Message(js);
expect(d.schemaFormat()).to.equal('application/schema+json;version=draft-07');
expect(d.schemaFormat()).to.equal('mySchema');
});
});

Expand Down
8 changes: 8 additions & 0 deletions test/parse_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,4 +663,12 @@ describe('registerSchemaParser()', function() {
parser.registerSchemaParser(parserModule);
}, expectedErrorObject);
});

it('should show that for 2.0 default schema format is 2.0 and for 2.1 it is 2.1', async function() {
const result20 = await parser.parse(inputYAML, { path: __filename });
const result21 = await parser.parse(fs.readFileSync(path.resolve(__dirname, './good/asyncapi-messages-example.yml'), 'utf8'), { path: __filename });
console.log(result20.channel('mychannel').publish().messages()[0]._json);
expect(result20.channel('mychannel').publish().messages()[0].schemaFormat()).to.equal('application/vnd.aai.asyncapi;version=2.0.0');
expect(result21.channel('myChannel').subscribe().messages()[0].schemaFormat()).to.equal('application/vnd.aai.asyncapi;version=2.1.0');
});
});
2 changes: 1 addition & 1 deletion test/wrong/invalid-asyncapi-messages-example.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
asyncapi: 2.0.0
asyncapi: 2.1.0
info:
title: My API
version: '1.0.0'
Expand Down

0 comments on commit a17d6d1

Please sign in to comment.