parser-js icon indicating copy to clipboard operation
parser-js copied to clipboard

parser doesn't validates the `$ref` links

Open KhudaDad414 opened this issue 4 years ago • 8 comments

Bug Description

If there is a typo in $ref value the parser should throw an error instead of passing the actual $ref.

How to Reproduce

const parser = require('@asyncapi/parser');

parser.parse(`
asyncapi: 2.0.0
info:
  title: Streetlights API
  version: '1.0.0'
channels:
  smartylighting/event/lighting/measured:
    subscribe:
      message:
        $ref: "#components/messages/message1"
components:
  messages:
    message1:
      name: unusedMessage
      title: This is most definityly a message.
`).then((doc) => {
  console.log(doc.channel('smartylighting/event/lighting/measured').subscribe().message().json());
});

Actual Result

{
  '$ref': '#components/messages/message1',
  schemaFormat: 'application/vnd.aai.asyncapi;version=2.0.0',
  'x-parser-message-parsed': true,
  'x-parser-message-name': '<anonymous-message-1>'
}

Expected Result

It should give an error message saying that my $ref path is broken or does not exist.

KhudaDad414 avatar Sep 09 '21 12:09 KhudaDad414

Since we don't want errors to be thrown but rather bundled together, to throw one big error in the end (I would assume so? git blame @derberg @fmvilas), this option is set to true: https://github.com/asyncapi/parser-js/blob/235efd5cb33df00c206bea3e9b09be9cd9587d87/lib/parser.js#L153

However, we never check that the dereference succedded, not sure if the dereference call actually returns any content, docs say nothing.

jonaslagoni avatar Sep 09 '21 13:09 jonaslagoni

Yeah, the idea is that it continues to throw one big error at the end. I'm now wondering if continueOnError actually does that 🤔

fmvilas avatar Sep 14 '21 07:09 fmvilas

I'm now wondering if continueOnError actually does that

it does, it gives an array of all errors

continueOnError: true

it is just an option in refs parser library that we leverage. As you know, when I added custom validations, I did not follow this principle, as far as I remember, my bad. The same option to group errors is also used in Ajv.

hard to say why there is no error, would require some debugging

derberg avatar Sep 14 '21 07:09 derberg

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] avatar Jan 13 '22 00:01 github-actions[bot]

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] avatar May 14 '22 00:05 github-actions[bot]

Can we solve this with a pattern field on the JSON Schema Reference Object ?

smoya avatar Jul 11 '22 14:07 smoya

dunno, maybe there is already some super bullet-proof regex created by the community that can validate $ref as it can represent, link to file, URL or location inside the existing document. It definitely exceeds my knowledge of regex 😄

derberg avatar Jul 11 '22 15:07 derberg

Otherwise, AJV should throw an error (MissingRefError) anyway. See this example I built: https://onecompiler.com/nodejs/3y9pgk7py

smoya avatar Jul 11 '22 16:07 smoya

Issue is resolved in v2 ParserJS - now in https://github.com/asyncapi/parser-js/tree/next-major branch thanks to Spectral. Pending to add a test.

cc @magicmatatjahu

smoya avatar Oct 25 '22 09:10 smoya

Resolved by https://github.com/asyncapi/parser-js/pull/676

magicmatatjahu avatar Nov 15 '22 07:11 magicmatatjahu

:tada: This issue has been resolved in version 2.0.0-next-major.11 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

asyncapi-bot avatar Dec 13 '22 11:12 asyncapi-bot