Skip to content

Commit

Permalink
add test reproducing the error
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRamosAcosta committed Nov 17, 2024
1 parent 3493290 commit 8f686f5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/parser/test/mocks/UserRegistered.yml
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions packages/parser/test/mocks/simple-with-external-refs.yaml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions packages/parser/test/parser.spec.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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");
});
});

0 comments on commit 8f686f5

Please sign in to comment.