From 8f686f53f37f129213d6e7bbfee02edffc4a50de Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Sun, 17 Nov 2024 12:46:00 +0100 Subject: [PATCH] add test reproducing the error --- packages/parser/test/mocks/UserRegistered.yml | 16 +++++++ .../test/mocks/simple-with-external-refs.yaml | 42 +++++++++++++++++++ packages/parser/test/parser.spec.ts | 19 +++++++++ 3 files changed, 77 insertions(+) create mode 100644 packages/parser/test/mocks/UserRegistered.yml create mode 100644 packages/parser/test/mocks/simple-with-external-refs.yaml diff --git a/packages/parser/test/mocks/UserRegistered.yml b/packages/parser/test/mocks/UserRegistered.yml new file mode 100644 index 000000000..a3bcd840d --- /dev/null +++ b/packages/parser/test/mocks/UserRegistered.yml @@ -0,0 +1,16 @@ +components: + schemas: + UserRegisteredType: + type: string + enum: [ UserRegistered ] + UserRegistered: + type: object + additionalProperties: false + required: + - type + - name + properties: + type: + $ref: "#/components/schemas/UserRegisteredType" + name: + type: string diff --git a/packages/parser/test/mocks/simple-with-external-refs.yaml b/packages/parser/test/mocks/simple-with-external-refs.yaml new file mode 100644 index 000000000..affdbd71f --- /dev/null +++ b/packages/parser/test/mocks/simple-with-external-refs.yaml @@ -0,0 +1,42 @@ +asyncapi: '3.0.0' +info: + title: User API + version: '1.0.0' + +servers: + local: + host: localhost:3000 + protocol: ws + +channels: + onUserRegistered: + messages: + UserRegistered: + name: UserRegistered + title: UserRegistered + payload: + $ref: 'UserRegistered.yml#/components/schemas/UserRegistered' + onUserLogged: + messages: + UserLogged: + name: UserLogged + title: UserLogged + payload: + $ref: '#/components/schemas/UserLogged' + +components: + schemas: + UserLoggedType: + type: string + enum: [UserLogged] + UserLogged: + type: object + additionalProperties: false + required: + - type + - name + properties: + type: + $ref: '#/components/schemas/UserLoggedType' + name: + type: string diff --git a/packages/parser/test/parser.spec.ts b/packages/parser/test/parser.spec.ts index 5c82fde6c..b47da8cda 100644 --- a/packages/parser/test/parser.spec.ts +++ b/packages/parser/test/parser.spec.ts @@ -1,6 +1,9 @@ +import path from 'path'; +import fs from 'fs'; import { Spectral } from '@stoplight/spectral-core'; import { Parser } from '../src/parser'; import { AsyncAPISchemaParser } from '../src/schema-parser/asyncapi-schema-parser'; +import { AsyncAPIDocumentInterface } from "../src"; describe('Parser class', function() { it('should create Parser instance', async function() { @@ -18,4 +21,20 @@ describe('Parser class', function() { parser.registerSchemaParser(AsyncAPISchemaParser()); expect((parser as any).parserRegistry.size).toBeGreaterThan(1); }); + + it('should give correct names to external references', async function() { + const parser = new Parser(); + const source = path.resolve(__dirname, './mocks/simple-with-external-refs.yaml'); + const nestedSchemas = fs.readFileSync(source, 'utf8'); + + const { document } = await parser.parse(nestedSchemas, { source }); + + const channels = (document as AsyncAPIDocumentInterface).channels().all(); + const onUserRegisteredChannel = channels[0] + const onUserLoggedChannel = channels[1] + const userRegisteredMessagePayload = onUserRegisteredChannel.messages().all()[0].payload()?.json() as any; + const userLoggedMessagePayload = onUserLoggedChannel.messages().all()[0].payload()?.json() as any; + expect(userRegisteredMessagePayload?.["x-parser-schema-id"]).toBe("UserRegistered"); + expect(userLoggedMessagePayload?.["x-parser-schema-id"]).toBe("UserLogged"); + }); });