Skip to content

Commit

Permalink
feat: add x-parser-spec-parsed extension (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Apr 23, 2021
1 parent 01d264c commit da21a70
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/anonymousNaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function assignNameToAnonymousMessages(doc) {
*/
function addNameToKey(messages, number) {
messages.forEach(m => {
if (m.name() === undefined) {
if (m.name() === undefined && m.ext(xParserMessageName) === undefined) {
m.json()[String(xParserMessageName)] = `<anonymous-message-${number}>`;
}
});
Expand Down
3 changes: 3 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const xParserSpecParsed = 'x-parser-spec-parsed';
const xParserMessageName = 'x-parser-message-name';
const xParserSchemaId = 'x-parser-schema-id';
const xParserCircle = 'x-parser-circular';
const xParserCircleProps = 'x-parser-circular-props';

module.exports = {
xParserSpecParsed,
xParserMessageName,
xParserSchemaId,
xParserCircle,
Expand Down
16 changes: 14 additions & 2 deletions lib/models/asyncapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Components = require('./components');
const MixinExternalDocs = require('../mixins/external-docs');
const MixinTags = require('../mixins/tags');
const MixinSpecificationExtensions = require('../mixins/specification-extensions');
const {xParserCircle, xParserCircleProps} = require('../constants');
const {xParserSpecParsed, xParserCircle, xParserCircleProps} = require('../constants');
const {assignNameToAnonymousMessages, assignNameToComponentMessages, assignUidToComponentSchemas, assignUidToParameterSchemas, assignIdToAnonymousSchemas} = require('../anonymousNaming');
const {traverseAsyncApiDocument, SchemaIteratorCallbackType} = require('../iterators');

Expand All @@ -23,16 +23,28 @@ const {traverseAsyncApiDocument, SchemaIteratorCallbackType} = require('../itera
* @returns {AsyncAPIDocument}
*/
class AsyncAPIDocument extends Base {
/**
* @constructor
*/
constructor(...args) {
super(...args);

assignNameToAnonymousMessages(this);
if (this.ext(xParserSpecParsed) === true) {
return;
}

assignNameToComponentMessages(this);
assignNameToAnonymousMessages(this);

markCircularSchemas(this);
assignUidToComponentSchemas(this);
assignUidToParameterSchemas(this);
assignIdToAnonymousSchemas(this);

// We add `x-parser-spec-parsed=true` extension to determine that the specification is parsed and validated
// and when the specification is re-passed to the AsyncAPIDocument constructor,
// there is no need to perform the same operations.
this.json()[String(xParserSpecParsed)] = true;
}

/**
Expand Down
49 changes: 48 additions & 1 deletion test/models/asyncapi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,63 @@ const fs = require('fs');
const path = require('path');

const AsyncAPIDocument = require('../../lib/models/asyncapi');
const { xParserMessageName, xParserSchemaId } = require('../../lib/constants');

const { assertMixinTagsInheritance } = require('../mixins/tags_test');
const { assertMixinExternalDocsInheritance } = require('../mixins/external-docs_test');
const { assertMixinSpecificationExtensionsInheritance } = require('../mixins/specification-extensions_test');

describe('AsyncAPIDocument', function() {
describe('constructor', function() {
it('should not change assigned uids', function() {
const schema = {};
const message = {
payload: schema,
};

const inputDoc = {
channels: {
channel: {
subscribe: {
message,
},
publish: {
message: {
payload: {}
}
}
}
},
components: {
messages: {
someMessage: message,
},
schemas: {
someSchema: schema,
}
}
};

let d = new AsyncAPIDocument(inputDoc); // NOSONAR
d = new AsyncAPIDocument(JSON.parse(JSON.stringify(d.json()))); // NOSONAR

expect(d.json().channels.channel.subscribe.message[xParserMessageName]).to.be.equal('someMessage');
expect(d.json().channels.channel.subscribe.message.payload[xParserSchemaId]).to.be.equal('someSchema');

expect(d.json().channels.channel.publish.message[xParserMessageName]).to.be.equal('<anonymous-message-1>');
expect(d.json().channels.channel.publish.message.payload[xParserSchemaId]).to.be.equal('<anonymous-schema-1>');

expect(d.json().components.messages.someMessage[xParserMessageName]).to.be.equal('someMessage');
expect(d.json().components.messages.someMessage.payload[xParserSchemaId]).to.be.equal('someSchema');

expect(d.json().components.schemas.someSchema[xParserSchemaId]).to.be.equal('someSchema');
});
});

describe('assignUidToParameterSchemas()', function() {
it('should assign uids to parameters', function() {
const inputDoc = { channels: { 'smartylighting/{streetlightId}': { parameters: { streetlightId: { schema: { type: 'string' } } } } } };
const expectedDoc = { channels: { 'smartylighting/{streetlightId}': { parameters: { streetlightId: { schema: { type: 'string', 'x-parser-schema-id': '<anonymous-schema-1>' }, 'x-parser-schema-id': 'streetlightId' } } } } };
const expectedDoc = { channels: { 'smartylighting/{streetlightId}': { parameters: { streetlightId: { schema: { type: 'string', 'x-parser-schema-id': '<anonymous-schema-1>' }, 'x-parser-schema-id': 'streetlightId' } } } }, 'x-parser-spec-parsed': true };
const d = new AsyncAPIDocument(inputDoc);
expect(d.json()).to.be.deep.equal(expectedDoc);
});
Expand Down
2 changes: 1 addition & 1 deletion test/parseFromUrl_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { checkErrorWrapper } = require('./testsUtils');
chai.use(chaiAsPromised);
const expect = chai.expect;

const validOutputJSON = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"/test/tester":{"subscribe":{"message":{"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}}}';
const validOutputJSON = '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{"/test/tester":{"subscribe":{"message":{"schemaFormat":"application/vnd.aai.asyncapi;version=2.0.0","x-parser-message-parsed":true,"x-parser-message-name":"<anonymous-message-1>"}}}},"x-parser-spec-parsed":true}';

describe('parseFromUrl()', function() {
it('should parse YAML correctly from URL', async function() {
Expand Down
Loading

0 comments on commit da21a70

Please sign in to comment.