Skip to content

Commit

Permalink
feat: assigning schema id to parameter schemas in components (#324)
Browse files Browse the repository at this point in the history
* Fixed parameter schemas are not getting correct id
Co-authored-by: Lukasz Gornicki <lpgornicki@gmail.com>
  • Loading branch information
jonaslagoni authored Jun 24, 2021
1 parent f989c6a commit d05c912
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
33 changes: 29 additions & 4 deletions lib/anonymousNaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ function assignNameToComponentMessages(doc) {
}
}

/**
* Assign ids based on parameter keys.
*
* @private
* @param {Record<string,Schema>} parameterObject
*/
function assignIdToParameters(parameterObject) {
for (const [parameterKey, parameter] of Object.entries(parameterObject)) {
if (parameter.schema()) {
parameter.schema().json()[String(xParserSchemaId)] = parameterKey;
}
}
}

/**
* Assign parameter keys as uid for the parameter schema.
*
Expand All @@ -26,9 +40,7 @@ function assignNameToComponentMessages(doc) {
function assignUidToParameterSchemas(doc) {
doc.channelNames().forEach(channelName => {
const channel = doc.channel(channelName);
for (const [parameterKey, parameterSchema] of Object.entries(channel.parameters())) {
parameterSchema.json()[String(xParserSchemaId)] = parameterKey;
}
assignIdToParameters(channel.parameters());
});
}

Expand All @@ -45,6 +57,18 @@ function assignUidToComponentSchemas(doc) {
}
}
}

/**
* Assign uid to component parameters schemas
*
* @private
* @param {AsyncAPIDocument} doc
*/
function assignUidToComponentParameterSchemas(doc) {
if (doc.hasComponents()) {
assignIdToParameters(doc.components().parameters());
}
}

/**
* Assign anonymous names to nameless messages.
Expand Down Expand Up @@ -98,6 +122,7 @@ module.exports = {
assignNameToComponentMessages,
assignUidToParameterSchemas,
assignUidToComponentSchemas,
assignUidToComponentParameterSchemas,
assignNameToAnonymousMessages,
assignIdToAnonymousSchemas
};
};
3 changes: 2 additions & 1 deletion lib/models/asyncapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const MixinExternalDocs = require('../mixins/external-docs');
const MixinTags = require('../mixins/tags');
const MixinSpecificationExtensions = require('../mixins/specification-extensions');
const {xParserSpecParsed, xParserCircle, xParserCircleProps} = require('../constants');
const {assignNameToAnonymousMessages, assignNameToComponentMessages, assignUidToComponentSchemas, assignUidToParameterSchemas, assignIdToAnonymousSchemas} = require('../anonymousNaming');
const {assignNameToAnonymousMessages, assignNameToComponentMessages, assignUidToComponentSchemas, assignUidToParameterSchemas, assignIdToAnonymousSchemas, assignUidToComponentParameterSchemas} = require('../anonymousNaming');
const {traverseAsyncApiDocument, SchemaIteratorCallbackType} = require('../iterators');

/**
Expand Down Expand Up @@ -38,6 +38,7 @@ class AsyncAPIDocument extends Base {

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

Expand Down
11 changes: 10 additions & 1 deletion test/models/asyncapi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ describe('AsyncAPIDocument', function() {
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' } } } }, 'x-parser-spec-parsed': true };
const expectedDoc = { channels: { 'smartylighting/{streetlightId}': { parameters: { streetlightId: { schema: { type: 'string', 'x-parser-schema-id': 'streetlightId' } } } } }, 'x-parser-spec-parsed': true };
const d = new AsyncAPIDocument(inputDoc);
expect(d.json()).to.be.deep.equal(expectedDoc);
});
});

describe('assignUidToComponentParameterSchemas()', function() {
it('should assign uids to component parameters', function() {
const inputDoc = { channels: { 'smartylighting/{streetlightId}': {}, components: { parameters: { streetlightId: { schema: { type: 'string' } } } } } };
const expectedDoc = { channels: { 'smartylighting/{streetlightId}': {}, components: { parameters: {streetlightId: { schema: { type: 'string', '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

0 comments on commit d05c912

Please sign in to comment.