From 7a0fb3952f8d14c5e96c2a961f97a19d95fc819c Mon Sep 17 00:00:00 2001 From: Viacheslav Turovskyi Date: Fri, 28 Oct 2022 07:24:27 +0000 Subject: [PATCH 1/5] fix: add check that `$ref` value is a `string` (#92) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maciej Urbańczyk --- src/parser.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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('#'); } /** From 8fab69072589c468cdac9df12fada2865e0ab767 Mon Sep 17 00:00:00 2001 From: Viacheslav Turovskyi Date: Thu, 3 Nov 2022 08:09:17 +0000 Subject: [PATCH 2/5] fix: add check that `$ref` value is a `string` (#92) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maciej Urbańczyk --- tests/lib/index.spec.ts | 32 ++++++++++++++++++++++++++++++++ tests/wrong-ref-absent.yaml | 10 ++++++++++ tests/wrong-ref-not-string.yaml | 10 ++++++++++ 3 files changed, 52 insertions(+) create mode 100644 tests/wrong-ref-absent.yaml create mode 100644 tests/wrong-ref-not-string.yaml diff --git a/tests/lib/index.spec.ts b/tests/lib/index.spec.ts index 769ace5..d73aec8 100644 --- a/tests/lib/index.spec.ts +++ b/tests/lib/index.spec.ts @@ -40,4 +40,36 @@ 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']; + const response = await bundle( + files.map(file => + fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8') + ), + { + referenceIntoComponents: true, + } + ); + + const result = response.json(); + + expect(result).toBeDefined(); + }); + + test('should not throw if value of `$ref` is absent', async () => { + const files = ['./tests/wrong-ref-absent.yaml']; + const response = await bundle( + files.map(file => + fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8') + ), + { + referenceIntoComponents: true, + } + ); + + const result = response.json(); + + expect(result).toBeDefined(); + }); }); 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 From e42cf8ddc17915907740bbbaffb835d25085b956 Mon Sep 17 00:00:00 2001 From: Viacheslav Turovskyi Date: Thu, 3 Nov 2022 08:09:17 +0000 Subject: [PATCH 3/5] fix: add check that `$ref` value is a `string` (#92) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maciej Urbańczyk --- tests/lib/index.spec.ts | 44 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/tests/lib/index.spec.ts b/tests/lib/index.spec.ts index d73aec8..206bab0 100644 --- a/tests/lib/index.spec.ts +++ b/tests/lib/index.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, test } from '@jest/globals'; +import { describe, expect, test, jest } from '@jest/globals'; import bundle from '../../src'; import fs from 'fs'; import path from 'path'; @@ -43,33 +43,31 @@ describe('bundler should ', () => { test('should not throw if value of `$ref` is not a string', async () => { const files = ['./tests/wrong-ref-not-string.yaml']; - const response = await bundle( - files.map(file => - fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8') - ), - { - referenceIntoComponents: true, - } - ); - const result = response.json(); - - expect(result).toBeDefined(); + 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']; - const response = await bundle( - files.map(file => - fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8') - ), - { - referenceIntoComponents: true, - } - ); - const result = response.json(); - - expect(result).toBeDefined(); + expect( + await bundle( + files.map(file => + fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8') + ), + { + referenceIntoComponents: true, + } + ) + ).resolves; }); }); From 0e23d81fdd2cb14543d39d90d005d078de5b8728 Mon Sep 17 00:00:00 2001 From: Viacheslav Turovskyi Date: Thu, 3 Nov 2022 08:09:17 +0000 Subject: [PATCH 4/5] fix: add check that `$ref` value is a `string` (#92) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maciej Urbańczyk --- tests/lib/index.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/index.spec.ts b/tests/lib/index.spec.ts index 206bab0..71d2094 100644 --- a/tests/lib/index.spec.ts +++ b/tests/lib/index.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, test, jest } from '@jest/globals'; +import { describe, expect, test } from '@jest/globals'; import bundle from '../../src'; import fs from 'fs'; import path from 'path'; From f75d825a5c9e13672c4a05493c8f1132568e3e8b Mon Sep 17 00:00:00 2001 From: Viacheslav Turovskyi Date: Thu, 3 Nov 2022 08:09:17 +0000 Subject: [PATCH 5/5] fix: add check that `$ref` value is a `string` (#92) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maciej Urbańczyk --- tests/lib/index.spec.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/lib/index.spec.ts b/tests/lib/index.spec.ts index 71d2094..b285340 100644 --- a/tests/lib/index.spec.ts +++ b/tests/lib/index.spec.ts @@ -44,6 +44,9 @@ describe('bundler should ', () => { 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 => @@ -59,6 +62,9 @@ describe('bundler should ', () => { 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 =>