From b4a022447d993281553e67cd19db041d57601136 Mon Sep 17 00:00:00 2001 From: souvik Date: Tue, 29 Nov 2022 08:17:21 +0530 Subject: [PATCH] fix: resolve external references in base file (#102) --- src/index.ts | 2 ++ tests/base-option/asyncapi.yaml | 33 +++++++++++++++++++++++++++++++++ tests/base-option/base.yaml | 5 +++++ tests/base-option/camera.yaml | 10 ++++++++++ tests/base-option/lights.yaml | 10 ++++++++++ tests/base-option/messages.yaml | 18 ++++++++++++++++++ tests/lib/index.spec.ts | 14 +++++++++++++- 7 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tests/base-option/asyncapi.yaml create mode 100644 tests/base-option/base.yaml create mode 100644 tests/base-option/camera.yaml create mode 100644 tests/base-option/lights.yaml create mode 100644 tests/base-option/messages.yaml diff --git a/src/index.ts b/src/index.ts index 6f0a55b..105fd73 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import { toJS, resolve } from './util'; import { Document } from './document'; +import { parse } from './parser'; import type { AsyncAPIObject } from './spec-types'; @@ -73,6 +74,7 @@ import type { AsyncAPIObject } from './spec-types'; export default async function bundle(files: string[], options: any = {}) { if (typeof options.base !== 'undefined') { options.base = toJS(options.base); + await parse(options.base); } const parsedJsons = files.map(file => toJS(file)) as AsyncAPIObject[]; diff --git a/tests/base-option/asyncapi.yaml b/tests/base-option/asyncapi.yaml new file mode 100644 index 0000000..2ae0c9d --- /dev/null +++ b/tests/base-option/asyncapi.yaml @@ -0,0 +1,33 @@ +asyncapi: 2.5.0 +info: + title: Smart home applicance service + version: 2.0.0 + description: This is a API for contolling various smart appliance +channels: + lights/On: + subcribe: + message: + $ref: '#/components/messages/LightsOn' + camera/click: + subcribe: + message: + $ref: '#/components/messages/clickPhoto' +components: + messages: + LightsOn: + payload: + type: object + properties: + sourceId: + type: string + voltage: + type: number + clickPhoto: + type: object + properties: + bustMode: + type: boolean + zoomDepth: + type: number + delayTimer: + type: number diff --git a/tests/base-option/base.yaml b/tests/base-option/base.yaml new file mode 100644 index 0000000..8a4f57f --- /dev/null +++ b/tests/base-option/base.yaml @@ -0,0 +1,5 @@ +asyncapi: "2.5.0" +info: + title: Smart home applicance service + version: 2.0.0 + description: This is a API for contolling various smart appliance diff --git a/tests/base-option/camera.yaml b/tests/base-option/camera.yaml new file mode 100644 index 0000000..d298118 --- /dev/null +++ b/tests/base-option/camera.yaml @@ -0,0 +1,10 @@ +asyncapi: "2.5.0" +info: + title: Camera Service + version: 2.0.0 + description: This service is in charge of for controlling Camera functions +channels: + camera/click: + subcribe: + message: + $ref: './tests/base-option/messages.yaml#/messages/clickPhoto' \ No newline at end of file diff --git a/tests/base-option/lights.yaml b/tests/base-option/lights.yaml new file mode 100644 index 0000000..1fb1d09 --- /dev/null +++ b/tests/base-option/lights.yaml @@ -0,0 +1,10 @@ +asyncapi: "2.5.0" +info: + title: Lights Service + version: 1.0.0 + description: This service is in charge of for controlling lights +channels: + lights/On: + subcribe: + message: + $ref: './tests/base-option/messages.yaml#/messages/LightsOn' \ No newline at end of file diff --git a/tests/base-option/messages.yaml b/tests/base-option/messages.yaml new file mode 100644 index 0000000..d13dc3a --- /dev/null +++ b/tests/base-option/messages.yaml @@ -0,0 +1,18 @@ +messages: + LightsOn: + payload: + type: object + properties: + sourceId: + type: string + voltage: + type: number + clickPhoto: + type: object + properties: + bustMode: + type: boolean + zoomDepth: + type: number + delayTimer: + type: number diff --git a/tests/lib/index.spec.ts b/tests/lib/index.spec.ts index d34f33a..d027c54 100644 --- a/tests/lib/index.spec.ts +++ b/tests/lib/index.spec.ts @@ -21,7 +21,7 @@ describe('[integration testing] bundler should ', () => { validate: false, } ); - + expect(response).toBeDefined(); }); @@ -77,6 +77,18 @@ describe('[integration testing] bundler should ', () => { ) ).resolves; }); + + test('should be able to bundle base file', async () => { + const files = ['./tests/base-option/lights.yaml', './tests/base-option/camera.yaml'] + + expect( + await bundle( + files.map(file => fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8')), + { referenceIntoComponents: true, base: fs.readFileSync(path.resolve(process.cwd(), './tests/base-option/base.yaml'), 'utf-8') } + ) + ).resolves; + + }) }); describe('[unit testing]', () => {