From 2cad21bdf3ec89f84b1e677a635223bc62112cc3 Mon Sep 17 00:00:00 2001 From: Sergio Moya <1083296+smoya@users.noreply.github.com> Date: Thu, 10 Aug 2023 16:12:45 +0200 Subject: [PATCH] fix: return proper schemas in asyncapi.schemas() method --- src/models/v3/asyncapi.ts | 2 +- test/models/v3/asyncapi.spec.ts | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/models/v3/asyncapi.ts b/src/models/v3/asyncapi.ts index 4e3dad044..40f7dbf4f 100644 --- a/src/models/v3/asyncapi.ts +++ b/src/models/v3/asyncapi.ts @@ -90,7 +90,7 @@ export class AsyncAPIDocument extends BaseModel implements As } schemas() { - return null as any; + return this.__schemas(false); } securitySchemes(): SecuritySchemesInterface { diff --git a/test/models/v3/asyncapi.spec.ts b/test/models/v3/asyncapi.spec.ts index 62c6b0e76..db3b41d2d 100644 --- a/test/models/v3/asyncapi.spec.ts +++ b/test/models/v3/asyncapi.spec.ts @@ -4,6 +4,7 @@ import { Components } from '../../../src/models/v3/components'; import { Info } from '../../../src/models/v3/info'; import { Messages } from '../../../src/models/v3/messages'; import { Operations } from '../../../src/models/v3/operations'; +import { Schemas } from '../../../src/models/v3/schemas'; import { SecuritySchemes } from '../../../src/models/v3/security-schemes'; import { Servers } from '../../../src/models/v3/servers'; @@ -127,7 +128,25 @@ describe('AsyncAPIDocument model', function() { }); describe('.schemas()', function() { - it.todo('should return a collection of schemas'); + it('should return a collection of schemas', function() { + const doc = serializeInput({ channels: { userSignup: { address: 'user/signup', messages: { someMessage1: { payload: {}}, someMessage2: { payload: {} } } }, userLogout: { address: 'user/logout', messages: { someMessage3WithoutPayload: {} } } } }); + const d = new AsyncAPIDocument(doc); + expect(d.schemas()).toBeInstanceOf(Schemas); + expect(d.schemas()).toHaveLength(2); + }); + + it('should return only an "used" schemas (without schemas from components)', function() { + const doc = serializeInput({ channels: { userSignup: { address: 'user/signup', messages: { someMessage1: { payload: {}}, someMessage2: { payload: {} } } } }, components: { schemas: { schemaOne: {}, schemaTwo: {} } } }); + const d = new AsyncAPIDocument(doc); + expect(d.schemas()).toBeInstanceOf(Schemas); + expect(d.schemas()).toHaveLength(2); + }); + + it('should return a collection of schemas even if collection is empty', function() { + const doc = serializeInput({}); + const d = new AsyncAPIDocument(doc); + expect(d.schemas()).toBeInstanceOf(Schemas); + }); }); describe('.securitySchemes()', function() {