diff --git a/src/parser.ts b/src/parser.ts index 85cf768..6196dcc 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -41,8 +41,8 @@ function crawlChannelPropertiesForRefs(JSONSchema: AsyncAPIObject) { * @returns {boolean} * @private */ -function isExternalReference(ref: string) { - return !ref.startsWith('#'); +function isExternalReference(ref: string): boolean { + return typeof ref === 'string' && !ref.startsWith('#'); } /** diff --git a/tests/lib/index.spec.ts b/tests/lib/index.spec.ts index 769ace5..b285340 100644 --- a/tests/lib/index.spec.ts +++ b/tests/lib/index.spec.ts @@ -40,4 +40,40 @@ describe('bundler should ', () => { expect(message.$ref).toMatch('#/components/messages/UserSignedUp'); }); + + test('should not throw if value of `$ref` is not a string', async () => { + const files = ['./tests/wrong-ref-not-string.yaml']; + + // If async function `bundle()` resolved Promise successfully, that means it + // did not throw exception during process of execution, which is the + // objective of testing. + expect( + await bundle( + files.map(file => + fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8') + ), + { + referenceIntoComponents: true, + } + ) + ).resolves; + }); + + test('should not throw if value of `$ref` is absent', async () => { + const files = ['./tests/wrong-ref-absent.yaml']; + + // If async function `bundle()` resolved Promise successfully, that means it + // did not throw exception during process of execution, which is the + // objective of testing. + expect( + await bundle( + files.map(file => + fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8') + ), + { + referenceIntoComponents: true, + } + ) + ).resolves; + }); }); diff --git a/tests/wrong-ref-absent.yaml b/tests/wrong-ref-absent.yaml new file mode 100644 index 0000000..754ae6a --- /dev/null +++ b/tests/wrong-ref-absent.yaml @@ -0,0 +1,10 @@ +asyncapi: '2.2.0' +info: + title: Account Service + version: 1.0.0 + description: This service is in charge of processing user signups +channels: + user/signedup: + subscribe: + message: + $ref: \ No newline at end of file diff --git a/tests/wrong-ref-not-string.yaml b/tests/wrong-ref-not-string.yaml new file mode 100644 index 0000000..e70e242 --- /dev/null +++ b/tests/wrong-ref-not-string.yaml @@ -0,0 +1,10 @@ +asyncapi: '2.2.0' +info: + title: Account Service + version: 1.0.0 + description: This service is in charge of processing user signups +channels: + user/signedup: + subscribe: + message: + $ref: 5 \ No newline at end of file