Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add x-parser-spec-parsed extension #288

Merged
merged 9 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 11 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,25 @@ 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);

this.json()[String(xParserSpecParsed)] = true;
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const ParserError = require('./errors/parser-error');
const { validateChannels, validateServerVariables, validateOperationId, validateServerSecurity } = require('./customValidators.js');
const { toJS, findRefs, getLocationOf, improveAjvErrors } = require('./utils');
const AsyncAPIDocument = require('./models/asyncapi');
const { xParserSpecParsed } = require('./constants');

const DEFAULT_SCHEMA_FORMAT = 'application/vnd.aai.asyncapi;version=2.0.0';
const OPERATIONS = ['publish', 'subscribe'];
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);
d = new AsyncAPIDocument(JSON.parse(JSON.stringify(d.json())));

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
Loading