Skip to content

Commit

Permalink
fix: upgrade @asyncapi/specs (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Sep 13, 2022
1 parent 58d0505 commit a8d6283
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 16,191 deletions.
17 changes: 12 additions & 5 deletions lib/asyncapiSchemaFormatParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const cloneDeep = require('lodash.clonedeep');
const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
schemaId: 'id',
schemaId: 'auto',
logger: false,
});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
Expand Down Expand Up @@ -68,7 +68,8 @@ function getMimeTypes() {
function getValidator(version) {
let validate = ajv.getSchema(version);
if (!validate) {
ajv.addSchema(preparePayloadSchema(asyncapi[String(version)]), version);
const payloadSchema = preparePayloadSchema(asyncapi[String(version)], version);
ajv.addSchema(payloadSchema, version);
validate = ajv.getSchema(version);
}
return validate;
Expand All @@ -80,12 +81,18 @@ function getValidator(version) {
*
* @private
* @param {Object} asyncapiSchema AsyncAPI specification JSON Schema
* @param {Object} version AsyncAPI version.
* @returns {Object} valid JSON Schema document describing format of AsyncAPI-valid schema for message payload
*/
function preparePayloadSchema(asyncapiSchema) {
function preparePayloadSchema(asyncapiSchema, version) {
const payloadSchema = `http://asyncapi.com/definitions/${version}/schema.json`;
const definitions = asyncapiSchema.definitions;
// Remove the meta schemas because it is already present within Ajv, and it's not possible to add duplicate schemas.
delete definitions['http://json-schema.org/draft-07/schema'];
delete definitions['http://json-schema.org/draft-04/schema'];
return {
$ref: '#/definitions/schema',
definitions: asyncapiSchema.definitions
$ref: payloadSchema,
definitions
};
}

Expand Down
9 changes: 7 additions & 2 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ const xParserMessageParsed = 'x-parser-message-parsed';
const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
schemaId: 'id',
schemaId: 'auto',
logger: false,
validateSchema: true,
});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));

Expand Down Expand Up @@ -186,7 +187,11 @@ async function handleCircularRefs(refParser, parsedJSON, initialFormat, asyncapi
function getValidator(version) {
let validate = ajv.getSchema(version);
if (!validate) {
ajv.addSchema(asyncapi[String(version)], version);
const asyncapiSchema = asyncapi[String(version)];
// Remove the meta schemas because it is already present within Ajv, and it's not possible to add duplicate schemas.
delete asyncapiSchema.definitions['http://json-schema.org/draft-07/schema'];
delete asyncapiSchema.definitions['http://json-schema.org/draft-04/schema'];
ajv.addSchema(asyncapiSchema, version);
validate = ajv.getSchema(version);
}
return validate;
Expand Down
Loading

0 comments on commit a8d6283

Please sign in to comment.