@asyncapi/parser
Advanced tools
Comparing version 1.15.0-2022-04-release.3 to 1.15.0-2022-04-release.4
@@ -178,2 +178,66 @@ const ParserError = require('./errors/parser-error'); | ||
/** | ||
* Validates if messageIds are duplicated in the document | ||
* | ||
* @private | ||
* @param {Object} parsedJSON parsed AsyncAPI document | ||
* @param {String} asyncapiYAMLorJSON AsyncAPI document in string | ||
* @param {String} initialFormat information of the document was originally JSON or YAML | ||
* @returns {Boolean} true in case the document is valid, otherwise throws {@link ParserError} | ||
*/ | ||
function validateMessageId( | ||
parsedJSON, | ||
asyncapiYAMLorJSON, | ||
initialFormat, | ||
operations | ||
) { | ||
const chnls = parsedJSON.channels; | ||
if (!chnls) return true; | ||
const chnlsMap = new Map(Object.entries(chnls)); | ||
//it is a map of paths, the one that is a duplicate and the one that is duplicated | ||
const duplicatedMessages = new Map(); | ||
//is is a 2-dimensional array that holds information with messageId value and its path | ||
const allMessages = []; | ||
const addDuplicateToMap = (msg, channelName, opName, oneOf = '') => { | ||
const messageId = msg.messageId; | ||
if (!messageId) return; | ||
const messagePath = `${tilde(channelName)}/${opName}/message${oneOf}/messageId`; | ||
const isMessageIdDuplicated = allMessages.find(v => v[0] === messageId); | ||
if (!isMessageIdDuplicated) | ||
return allMessages.push([messageId, messagePath]); | ||
//isMessageIdDuplicated always holds one record and it is an array of paths, the one that is a duplicate and the one that is duplicated | ||
duplicatedMessages.set(messagePath, isMessageIdDuplicated[1]); | ||
}; | ||
chnlsMap.forEach((chnlObj, chnlName) => { | ||
operations.forEach((opName) => { | ||
const op = chnlObj[String(opName)]; | ||
if (op && op.message) { | ||
if (op.message.oneOf) op.message.oneOf.forEach((msg, index) => addDuplicateToMap(msg, chnlName, opName , `/oneOf/${index}`)); | ||
else addDuplicateToMap(op.message, chnlName, opName); | ||
} | ||
}); | ||
}); | ||
if (duplicatedMessages.size) { | ||
throw new ParserError({ | ||
type: validationError, | ||
title: 'messageId must be unique across all the messages.', | ||
parsedJSON, | ||
validationErrors: groupValidationErrors( | ||
'channels', | ||
'is a duplicate of', | ||
duplicatedMessages, | ||
asyncapiYAMLorJSON, | ||
initialFormat | ||
), | ||
}); | ||
} | ||
return true; | ||
} | ||
/** | ||
* Validates if server security is declared properly and the name has a corresponding security schema definition in components with the same name | ||
@@ -615,2 +679,3 @@ * | ||
validateOperationId, | ||
validateMessageId, | ||
validateServerSecurity, | ||
@@ -617,0 +682,0 @@ validateChannels, |
@@ -44,2 +44,9 @@ const { getMapValueOfType, mix } = require('./utils'); | ||
/** | ||
* @returns {string} | ||
*/ | ||
id() { | ||
return this._json.messageId; | ||
} | ||
/** | ||
* @returns {CorrelationId} | ||
@@ -46,0 +53,0 @@ */ |
@@ -17,3 +17,3 @@ const MessageTrait = require('./message-trait'); | ||
uid() { | ||
return this.name() || this.ext('x-parser-message-name') || Buffer.from(JSON.stringify(this._json)).toString('base64'); | ||
return this.id() || this.name() || this.ext('x-parser-message-name') || Buffer.from(JSON.stringify(this._json)).toString('base64'); | ||
} | ||
@@ -20,0 +20,0 @@ |
@@ -8,3 +8,3 @@ const path = require('path'); | ||
const ParserError = require('./errors/parser-error'); | ||
const { validateChannels, validateTags, validateServerVariables, validateOperationId, validateServerSecurity } = require('./customValidators.js'); | ||
const { validateChannels, validateTags, validateServerVariables, validateOperationId, validateServerSecurity, validateMessageId } = require('./customValidators.js'); | ||
const { toJS, findRefs, getLocationOf, improveAjvErrors, getDefaultSchemaFormat } = require('./utils'); | ||
@@ -205,2 +205,3 @@ const AsyncAPIDocument = require('./models/asyncapi'); | ||
validateOperationId(parsedJSON, asyncapiYAMLorJSON, initialFormat, OPERATIONS); | ||
validateMessageId(parsedJSON, asyncapiYAMLorJSON, initialFormat, OPERATIONS); | ||
@@ -207,0 +208,0 @@ await customComponentsMsgOperations(parsedJSON, asyncapiYAMLorJSON, initialFormat, options); |
{ | ||
"name": "@asyncapi/parser", | ||
"version": "1.15.0-2022-04-release.3", | ||
"version": "1.15.0-2022-04-release.4", | ||
"description": "JavaScript AsyncAPI parser.", | ||
@@ -70,3 +70,3 @@ "main": "lib/index.js", | ||
"@apidevtools/json-schema-ref-parser": "^9.0.6", | ||
"@asyncapi/specs": "^v2.14.0-2022-04-release.2", | ||
"@asyncapi/specs": "^v2.14.0-2022-04-release.3", | ||
"@fmvilas/pseudo-yaml-ast": "^0.3.1", | ||
@@ -73,0 +73,0 @@ "ajv": "^6.10.1", |
@@ -677,2 +677,3 @@ /** | ||
header(name: string): Schema; | ||
id(): string; | ||
correlationId(): CorrelationId; | ||
@@ -679,0 +680,0 @@ schemaFormat(): string; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1293531
8279