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

Little repro case of asyncapi schema related issues #158

Closed
wants to merge 1 commit into from
Closed

Little repro case of asyncapi schema related issues #158

wants to merge 1 commit into from

Conversation

nulltoken
Copy link

@nulltoken nulltoken commented Mar 12, 2020

While working on stoplightio/spectral#974, I tried to leverage json-schema-ref-parser to process the asyncapi schema.

However, I've hit some walls. I'm sharing some repro cases.

Out of the three tests cases, two fails (full dereferencing and bundling) as the resulting schema cannot be processed by Ajv.

 1) can process ayncapi schema so that AJV does not choke

       through full dereferencing:

     TypeError: Converting circular structure to JSON

      at stringify (node_modules/fast-json-stable-stringify/index.js:42:19)
      at stringify (node_modules/fast-json-stable-stringify/index.js:50:25)
      at stringify (node_modules/fast-json-stable-stringify/index.js:50:25)
      at stringify (node_modules/fast-json-stable-stringify/index.js:50:25)
      at stringify (node_modules/fast-json-stable-stringify/index.js:33:24)
      at stringify (node_modules/fast-json-stable-stringify/index.js:50:25)
      at stringify (node_modules/fast-json-stable-stringify/index.js:50:25)
      at stringify (node_modules/fast-json-stable-stringify/index.js:50:25)
      at stringify (node_modules/fast-json-stable-stringify/index.js:50:25)
      at stringify (node_modules/fast-json-stable-stringify/index.js:50:25)
      at stringify (node_modules/fast-json-stable-stringify/index.js:50:25)
      at stringify (node_modules/fast-json-stable-stringify/index.js:50:25)
      at stringify (node_modules/fast-json-stable-stringify/index.js:50:25)
      at module.exports (node_modules/fast-json-stable-stringify/index.js:58:7)
      at Ajv._addSchema (node_modules/ajv/lib/ajv.js:294:30)
      at Ajv.compile (node_modules/ajv/lib/ajv.js:112:24)
      at validate (test/specs/asyncapi/canHandle.spec.js:50:28)
      at Context.it (test/specs/asyncapi/canHandle.spec.js:30:5)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:189:7)

  2) can process ayncapi schema so that AJV does not choke

       through bundling:

     Error: id "http://json-schema.org/draft-07/schema" resolves to more than one schema

      at /home/travis/build/APIDevTools/json-schema-ref-parser/node_modules/ajv/lib/compile/resolve.js:254:17
      at _traverse (node_modules/json-schema-traverse/index.js:65:5)
      at _traverse (node_modules/json-schema-traverse/index.js:71:13)
      at _traverse (node_modules/json-schema-traverse/index.js:76:13)
      at module.exports (node_modules/json-schema-traverse/index.js:14:3)
      at Ajv.resolveIds (node_modules/ajv/lib/compile/resolve.js:239:3)
      at Ajv._addSchema (node_modules/ajv/lib/ajv.js:308:31)
      at Ajv.compile (node_modules/ajv/lib/ajv.js:112:24)
      at validate (test/specs/asyncapi/canHandle.spec.js:50:28)
      at Context.it (test/specs/asyncapi/canHandle.spec.js:40:5)
      at <anonymous>

Is that expected? Did I do something wrong?

Any help or guidance would be greatly appreciated 😄

/cc @philsturgeon @fmvilas

Copy link

@fmvilas fmvilas left a comment

Choose a reason for hiding this comment

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

Left some hints. Hope they help!

Also, is there any reason you're using this library and ajv directly instead of using the JS AsyncAPI parser? It's doing this for you :)

function validate (schema, doc) {
const ajv = new AJV({
allErrors: true,
schemaId: "auto",
Copy link

Choose a reason for hiding this comment

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

I think this one should be:

Suggested change
schemaId: "auto",
schemaId: "id",

Copy link
Author

Choose a reason for hiding this comment

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

@fmvilas Yep. That did it. Switching to "id" did it and solved the bundling issue. Thanks for the hint!

"oneOf": {
"type": "array",
"items": {
"$ref": "#/definitions/message"
Copy link

Choose a reason for hiding this comment

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

And the "through full dereferencing" test may be failing because there's a circular dependency on the schema here?

Copy link
Author

Choose a reason for hiding this comment

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

Indeed 😉

From what I read, Ajv seemed to never liked cirdular references (although the latest version seems to support them "between schemas"). In numerous issues @epoberezkin mentioned that starting from a serializable schema was the way to go. In that perspective, I had lots of hope for the bundle() method. But it doesn't seem to work either.

@nulltoken
Copy link
Author

Left some hints. Hope they help!

@fmvilas Thanks!

Also, is there any reason you're using this library and ajv directly instead of using the JS AsyncAPI parser? It's doing this for you :)

Spectral runs its own schema validation mechanism in a as generic as possible way to support multiple schemas and plugging this library in as is (while respecting all Spectral use cases), wasn't easy to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants