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

fix: upgrade @asyncapi/specs #423

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a865a0c
Prepared for new spec repo.
jonaslagoni Dec 9, 2021
15949e4
Prepared for new spec repo.
jonaslagoni Dec 9, 2021
12c8fb1
Added file extension
jonaslagoni Dec 10, 2021
09a050a
Merge branch 'master' into feature/update_spec_json_schema
jonaslagoni Mar 14, 2022
d7cfbf4
Update package.json
jonaslagoni Mar 15, 2022
973e9b2
Merge branch 'master' into feature/update_spec_json_schema
jonaslagoni Mar 23, 2022
4842a82
Updated dependency
jonaslagoni Mar 24, 2022
8af4a34
WIP schema format parser
jonaslagoni Mar 24, 2022
9d74fb4
Merge branch 'master' into feature/update_spec_json_schema
smoya Mar 28, 2022
bcb5216
removed bundled json schema
jonaslagoni Mar 30, 2022
43ad46c
Merge branch 'master' into feature/update_spec_json_schema
jonaslagoni Mar 30, 2022
d22ad29
removed draft 4 meta schema
jonaslagoni Mar 30, 2022
7dcb8a9
Updated with new spec version
jonaslagoni Apr 6, 2022
b3361a0
Merge branch 'master' into feature/update_spec_json_schema
jonaslagoni Apr 6, 2022
acdd428
updated to new package
jonaslagoni May 16, 2022
fc1c54c
Merge branch 'master-main' into feature/update_spec_json_schema
jonaslagoni May 16, 2022
89c6db0
updated packagelock
jonaslagoni May 16, 2022
4819866
updated package
jonaslagoni May 16, 2022
67a25e3
Update lib/asyncapiSchemaFormatParser.js
jonaslagoni May 19, 2022
244f0f6
Merge branch 'master' into feature/update_spec_json_schema
jonaslagoni May 30, 2022
933058d
Merge branch 'master' into feature/update_spec_json_schema
jonaslagoni Jun 8, 2022
c83b53d
Merge branch 'master-github-upstream' into feature/update_spec_json_s…
jonaslagoni Sep 13, 2022
d5ef367
update package-lock
jonaslagoni Sep 13, 2022
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
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`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't we grab it from @asyncapi/specs instead of remote?

sorry if that was answered somewhere already. Also, I do not remember really much from times when I created asyncapiSchemaFormatParser.js

just dereferencing something in the parser from remote is going to slow down parsing, or?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using the local one from @asyncapi/specs 🙂

It is only what the reference is called. Ajv automatically matches it with the schema inside definitions 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need http://asyncapi.com/definitions/ published before we merge?
tbh I'm not 100% sure how these $id work...to complicated for me 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at all, the $ids when bundled together like so, do not need external access to the schemas 🙂 Because validation tools must look for the reference within before reaching externally.

We don't need to expose them at all if everyone uses the bundled schemas, but say you want to use this one: https://github.com/asyncapi/spec-json-schemas/blob/master/definitions/2.4.0/asyncapi.json, it would need the schemas exposed on http://asyncapi.com/definitions/ as the schemas are not located within.

Does that make sense?

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