diff --git a/lib/utils.js b/lib/utils.js index 823dfdfce..e513bf93d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -18,7 +18,7 @@ const getAST = (asyncapiYAMLorJSON, initialFormat) => { const findNode = (obj, location) => { for (const key of location) { - obj = obj[utils.untilde(key)]; + obj = obj ? obj[utils.untilde(key)] : null; } return obj; }; diff --git a/test/parse_test.js b/test/parse_test.js index 8ca0ae09a..75eb690cd 100644 --- a/test/parse_test.js +++ b/test/parse_test.js @@ -410,6 +410,19 @@ describe('parse()', function() { }, expectedErrorObject); }); + it('should throw proper error even if issue is inside $refed file of a $refed file', async function() { + const expectedErrorObject = { + type: 'https://github.com/asyncapi/parser-js/schema-validation-errors', + title: 'This is not a valid AsyncAPI Schema Object.' + }; + + await checkErrorWrapper(async () => { + await parser.parse(fs.readFileSync(path.resolve(__dirname, './wrong/good-ref-to-broken-file.yaml'), 'utf8'), { + path: __filename, + }); + }, expectedErrorObject); + }); + it('should throw error if document is invalid YAML', async function() { const expectedErrorObject = { type: 'https://github.com/asyncapi/parser-js/invalid-yaml', diff --git a/test/wrong/good-ref-to-broken-file.yaml b/test/wrong/good-ref-to-broken-file.yaml new file mode 100644 index 000000000..1fa768daa --- /dev/null +++ b/test/wrong/good-ref-to-broken-file.yaml @@ -0,0 +1,9 @@ +asyncapi: 2.0.0 +info: + title: My API + version: '1.0.0' +channels: + mychannel: + publish: + message: + $ref: 'wrong/good-refed-file.yml' \ No newline at end of file diff --git a/test/wrong/good-refed-file.yml b/test/wrong/good-refed-file.yml new file mode 100644 index 000000000..d156ee3a9 --- /dev/null +++ b/test/wrong/good-refed-file.yml @@ -0,0 +1,2 @@ +payload: + $ref: refed-file-broken-schema.yml \ No newline at end of file diff --git a/test/wrong/refed-file-broken-schema.yml b/test/wrong/refed-file-broken-schema.yml new file mode 100644 index 000000000..1341bd105 --- /dev/null +++ b/test/wrong/refed-file-broken-schema.yml @@ -0,0 +1,4 @@ +type: string +properties: + name: + type: string \ No newline at end of file