Skip to content

Commit

Permalink
Merge branch 'master' into parseFromUrl-does-not-resolve-relative-ref…
Browse files Browse the repository at this point in the history
…erences-asyncapi#504
  • Loading branch information
jonaslagoni authored Sep 14, 2022
2 parents 3f11dc9 + c6c6c77 commit 9a912a4
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 16,193 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 @@ -26,8 +26,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 @@ -201,7 +202,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 9a912a4

Please sign in to comment.