Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: external referenced schema names #1064

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails. The actual value is <anonymous-schema-1>

expect(userLoggedMessagePayload?.["x-parser-schema-id"]).toBe("UserLogged");
});
});
Loading