Skip to content

Commit

Permalink
fix: resolve type mismatch in allServers and allChannels (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhudaDad414 authored Sep 1, 2023
1 parent 49183a4 commit 58fdca3
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/models/v2/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ export class AsyncAPIDocument extends BaseModel<v2.AsyncAPIObject> implements As
}

allServers(): ServersInterface {
const servers: ServerInterface[] = this.servers();
const servers: ServerInterface[] = this.servers().all();
this.components().servers().forEach(server =>
!servers.some(s => s.json() === server.json()) && servers.push(server)
);
return new Servers(servers);
}

allChannels(): ChannelsInterface {
const channels: ChannelInterface[] = this.channels();
const channels: ChannelInterface[] = this.channels().all();
this.components().channels().forEach(channel =>
!channels.some(c => c.json() === channel.json()) && channels.push(channel)
);
Expand Down
15 changes: 7 additions & 8 deletions src/models/v3/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,31 @@ export class AsyncAPIDocument extends BaseModel<v3.AsyncAPIObject> implements As
}

allServers(): ServersInterface {
const servers: ServerInterface[] = this.servers();
const servers: ServerInterface[] = this.servers().all();
this.components().servers().forEach(server =>
!servers.some(s => s.json() === server.json()) && servers.push(server)
);
return new Servers(servers);
}

allChannels(): ChannelsInterface {
const channels: ChannelInterface[] = this.channels();
const channels: ChannelInterface[] = this.channels().all();
this.components().channels().forEach(channel =>
!channels.some(c => c.json() === channel.json()) && channels.push(channel)
);
return new Channels(channels);
}

allOperations(): OperationsInterface {
const operations: OperationInterface[] = [];
this.allChannels().forEach(channel => operations.push(...channel.operations()));
const operations: OperationInterface[] = this.operations().all();
this.components().operations().forEach(operation =>
!operations.some(o => o.json() === operation.json()) && operations.push(operation)
);
return new Operations(operations);
}

allMessages(): MessagesInterface {
const messages: MessageInterface[] = [];
this.allOperations().forEach(operation => operation.messages().forEach(message => (
!messages.some(m => m.json() === message.json()) && messages.push(message)
)));
const messages: MessageInterface[] = this.messages().all();
this.components().messages().forEach(message => (
!messages.some(m => m.json() === message.json()) && messages.push(message)
));
Expand Down
11 changes: 11 additions & 0 deletions test/models/v2/asyncapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Operations } from '../../../src/models/v2/operations';
import { Schemas } from '../../../src/models/v2/schemas';
import { SecuritySchemes } from '../../../src/models/v2/security-schemes';
import { Servers } from '../../../src/models/v2/servers';
import { Collection } from '../../../src/models';

import { serializeInput, assertExtensions } from './utils';

Expand Down Expand Up @@ -189,6 +190,8 @@ describe('AsyncAPIDocument model', function() {
const doc = serializeInput<v2.AsyncAPIObject>({ servers: { development: {} } });
const d = new AsyncAPIDocument(doc);
expect(d.allServers()).toBeInstanceOf(Servers);
expect(d.allServers().all()).toBeInstanceOf(Array);
expect(d.allServers().all()).not.toBeInstanceOf(Collection);
expect(d.allServers()).toHaveLength(1);
expect(d.allServers().all()[0].id()).toEqual('development');
});
Expand All @@ -212,6 +215,8 @@ describe('AsyncAPIDocument model', function() {
const doc = serializeInput<v2.AsyncAPIObject>({ channels: { 'user/signup': {} } });
const d = new AsyncAPIDocument(doc);
expect(d.allChannels()).toBeInstanceOf(Channels);
expect(d.allChannels().all()).not.toBeInstanceOf(Collection);
expect(d.allChannels().all()).toBeInstanceOf(Array);
expect(d.allChannels()).toHaveLength(1);
expect(d.allChannels().all()[0].address()).toEqual('user/signup');
});
Expand All @@ -235,6 +240,8 @@ describe('AsyncAPIDocument model', function() {
const doc = serializeInput<v2.AsyncAPIObject>({ channels: { 'user/signup': { publish: {}, subscribe: {} }, 'user/logout': { publish: {} } } });
const d = new AsyncAPIDocument(doc);
expect(d.allOperations()).toBeInstanceOf(Operations);
expect(d.allOperations().all()).toBeInstanceOf(Array);
expect(d.allOperations().all()).not.toBeInstanceOf(Collection);
expect(d.allOperations()).toHaveLength(3);
});

Expand All @@ -257,6 +264,8 @@ describe('AsyncAPIDocument model', function() {
it('should return a collection of messages', function() {
const doc = serializeInput<v2.AsyncAPIObject>({ channels: { 'user/signup': { publish: { message: {} }, subscribe: { message: { oneOf: [{}, {}] } } }, 'user/logout': { publish: { message: {} } } } });
const d = new AsyncAPIDocument(doc);
expect(d.allMessages().all()).toBeInstanceOf(Array);
expect(d.allMessages().all()).not.toBeInstanceOf(Collection);
expect(d.allMessages()).toBeInstanceOf(Messages);
expect(d.allMessages()).toHaveLength(4);
});
Expand All @@ -282,6 +291,8 @@ describe('AsyncAPIDocument model', function() {
const doc = serializeInput<v2.AsyncAPIObject>({ channels: { 'user/signup': { publish: { message: { payload: {} } }, subscribe: { message: { oneOf: [{ payload: {} }, {}, { payload: {} }] } } }, 'user/logout': { publish: { message: { payload: {} } } } } });
const d = new AsyncAPIDocument(doc);
expect(d.allSchemas()).toBeInstanceOf(Schemas);
expect(d.allSchemas().all()).toBeInstanceOf(Array);
expect(d.allSchemas().all()).not.toBeInstanceOf(Collection);
expect(d.allSchemas()).toHaveLength(4);
});

Expand Down
125 changes: 125 additions & 0 deletions test/models/v3/asyncapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Servers } from '../../../src/models/v3/servers';
import { serializeInput, assertExtensions } from './utils';

import type { v3 } from '../../../src/spec-types';
import { Collection } from '../../../src/models';

describe('AsyncAPIDocument model', function() {
describe('.version()', function() {
Expand Down Expand Up @@ -164,6 +165,130 @@ describe('AsyncAPIDocument model', function() {
});
});

describe('.allMessages()', function() {
it('should return a collection of messages', function() {
const duplicatedMessage = {};
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { userSignup: { address: 'user/signup', messages: { someMessage1: {}, someMessage2: duplicatedMessage } }, userLogout: { address: 'user/logout', messages: { someMessage3: duplicatedMessage } } } });
const d = new AsyncAPIDocument(doc);
expect(d.allMessages().all()).toBeInstanceOf(Array);
expect(d.allMessages().all()).not.toBeInstanceOf(Collection);
expect(d.allMessages()).toBeInstanceOf(Messages);
expect(d.allMessages()).toHaveLength(2);
});

it('should return all messages (with messages from components)', function() {
const duplicatedMessage = {};
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { userSignup: { address: 'user/signup', messages: { someMessage1: {}, someMessage2: duplicatedMessage } }, userLogout: { address: 'user/logout', messages: { someMessage3: duplicatedMessage } } }, components: { messages: { someMessage4: {}, someMessage5: {} } }});
const d = new AsyncAPIDocument(doc);
expect(d.allMessages()).toBeInstanceOf(Messages);
expect(d.allMessages()).toHaveLength(4);
});

it('should return a collection of messages even if messages are not defined', function() {
const doc = serializeInput<v3.AsyncAPIObject>({});
const d = new AsyncAPIDocument(doc);
expect(d.allMessages()).toBeInstanceOf(Messages);
});
});

describe('.allSchemas()', function() {
it('should return a collection of schemas', function() {
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { userSignup: { address: 'user/signup', messages: { someMessage1: { payload: {}}, someMessage2: { payload: {} } } }, userLogout: { address: 'user/logout', messages: { someMessage3WithoutPayload: {} } } } });
const d = new AsyncAPIDocument(doc);
expect(d.allSchemas()).toBeInstanceOf(Schemas);
expect(d.allSchemas().all()).toBeInstanceOf(Array);
expect(d.allSchemas().all()).not.toBeInstanceOf(Collection);
expect(d.allSchemas()).toHaveLength(2);
});

it('should return all schemas (with schemas from components)', function() {
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { userSignup: { address: 'user/signup', messages: { someMessage1: { payload: {}}, someMessage2: { payload: {} } } } }, components: { schemas: { schemaOne: {}, schemaTwo: {} } } });
const d = new AsyncAPIDocument(doc);
expect(d.allSchemas()).toBeInstanceOf(Schemas);
expect(d.allSchemas()).toHaveLength(4);
});

it('should return a collection of schemas even if collection is empty', function() {
const doc = serializeInput<v3.AsyncAPIObject>({});
const d = new AsyncAPIDocument(doc);
expect(d.allSchemas()).toBeInstanceOf(Schemas);
});
});

describe('.allServers()', function() {
it('should return a collection of servers', function() {
const doc = serializeInput<v3.AsyncAPIObject>({ servers: { development: {} } });
const d = new AsyncAPIDocument(doc);
expect(d.allServers()).toBeInstanceOf(Servers);
expect(d.allServers().all()).toBeInstanceOf(Array);
expect(d.allServers().all()).not.toBeInstanceOf(Collection);
expect(d.allServers()).toHaveLength(1);
expect(d.allServers().all()[0].id()).toEqual('development');
});

it('should return all servers (with servers from components)', function() {
const doc = serializeInput<v3.AsyncAPIObject>({ servers: { production: {} }, components: { servers: { development: {} } } });
const d = new AsyncAPIDocument(doc);
expect(d.allServers()).toBeInstanceOf(Servers);
expect(d.allServers()).toHaveLength(2);
});

it('should return a collection of servers even if servers are not defined', function() {
const doc = serializeInput<v3.AsyncAPIObject>({});
const d = new AsyncAPIDocument(doc);
expect(d.allServers()).toBeInstanceOf(Servers);
});
});

describe('.allChannels()', function() {
it('should return a collection of channels', function() {
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { 'user/signup': {} } });
const d = new AsyncAPIDocument(doc);
expect(d.allChannels()).toBeInstanceOf(Channels);
expect(d.allChannels().all()).not.toBeInstanceOf(Collection);
expect(d.allChannels().all()).toBeInstanceOf(Array);
expect(d.allChannels()).toHaveLength(1);
});

it('should return all channels (with channels from components)', function() {
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { 'user/signup': {} }, components: { channels: { someChannel1: {}, someChannel2: {} } } });
const d = new AsyncAPIDocument(doc);
expect(d.allChannels()).toBeInstanceOf(Channels);
expect(d.allChannels()).toHaveLength(3);
});

it('should return a collection of channels even if channels are not defined', function() {
const doc = serializeInput<v3.AsyncAPIObject>({});
const d = new AsyncAPIDocument(doc);
expect(d.allChannels()).toBeInstanceOf(Channels);
});
});

describe('.allOperations()', function() {
it('should return a collection of operations', function() {
const doc = serializeInput<v3.AsyncAPIObject>({ operations: { userSignup: {}, userLogout: {} } });
const d = new AsyncAPIDocument(doc);
expect(d.allOperations()).toBeInstanceOf(Operations);
expect(d.allOperations().all()).toBeInstanceOf(Array);
expect(d.allOperations().all()).not.toBeInstanceOf(Collection);
expect(d.allOperations()).toHaveLength(2);
});

it('should return all operations (with operations from components)', function() {
const duplicatedOperation = { };
const doc = serializeInput<v3.AsyncAPIObject>({ operations: { userSignup: duplicatedOperation, userLogout: {} }, components: { operations: { someOperation1: duplicatedOperation, someOperation2: {} } } });
const d = new AsyncAPIDocument(doc);
expect(d.allOperations()).toBeInstanceOf(Operations);
expect(d.allOperations()).toHaveLength(3);
});

it('should return a collection of operations even if operations are not defined', function() {
const doc = serializeInput<v3.AsyncAPIObject>({});
const d = new AsyncAPIDocument(doc);
expect(d.allOperations()).toBeInstanceOf(Operations);
});
});

describe('.components()', function() {
it('should return a components model', function() {
const doc = serializeInput<v3.AsyncAPIObject>({ components: {} });
Expand Down

0 comments on commit 58fdca3

Please sign in to comment.