Skip to content

Commit 58fdca3

Browse files
authored
fix: resolve type mismatch in allServers and allChannels (#854)
1 parent 49183a4 commit 58fdca3

File tree

4 files changed

+145
-10
lines changed

4 files changed

+145
-10
lines changed

src/models/v2/asyncapi.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ export class AsyncAPIDocument extends BaseModel<v2.AsyncAPIObject> implements As
9797
}
9898

9999
allServers(): ServersInterface {
100-
const servers: ServerInterface[] = this.servers();
100+
const servers: ServerInterface[] = this.servers().all();
101101
this.components().servers().forEach(server =>
102102
!servers.some(s => s.json() === server.json()) && servers.push(server)
103103
);
104104
return new Servers(servers);
105105
}
106106

107107
allChannels(): ChannelsInterface {
108-
const channels: ChannelInterface[] = this.channels();
108+
const channels: ChannelInterface[] = this.channels().all();
109109
this.components().channels().forEach(channel =>
110110
!channels.some(c => c.json() === channel.json()) && channels.push(channel)
111111
);

src/models/v3/asyncapi.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,31 @@ export class AsyncAPIDocument extends BaseModel<v3.AsyncAPIObject> implements As
106106
}
107107

108108
allServers(): ServersInterface {
109-
const servers: ServerInterface[] = this.servers();
109+
const servers: ServerInterface[] = this.servers().all();
110110
this.components().servers().forEach(server =>
111111
!servers.some(s => s.json() === server.json()) && servers.push(server)
112112
);
113113
return new Servers(servers);
114114
}
115115

116116
allChannels(): ChannelsInterface {
117-
const channels: ChannelInterface[] = this.channels();
117+
const channels: ChannelInterface[] = this.channels().all();
118118
this.components().channels().forEach(channel =>
119119
!channels.some(c => c.json() === channel.json()) && channels.push(channel)
120120
);
121121
return new Channels(channels);
122122
}
123123

124124
allOperations(): OperationsInterface {
125-
const operations: OperationInterface[] = [];
126-
this.allChannels().forEach(channel => operations.push(...channel.operations()));
125+
const operations: OperationInterface[] = this.operations().all();
126+
this.components().operations().forEach(operation =>
127+
!operations.some(o => o.json() === operation.json()) && operations.push(operation)
128+
);
127129
return new Operations(operations);
128130
}
129131

130132
allMessages(): MessagesInterface {
131-
const messages: MessageInterface[] = [];
132-
this.allOperations().forEach(operation => operation.messages().forEach(message => (
133-
!messages.some(m => m.json() === message.json()) && messages.push(message)
134-
)));
133+
const messages: MessageInterface[] = this.messages().all();
135134
this.components().messages().forEach(message => (
136135
!messages.some(m => m.json() === message.json()) && messages.push(message)
137136
));

test/models/v2/asyncapi.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Operations } from '../../../src/models/v2/operations';
77
import { Schemas } from '../../../src/models/v2/schemas';
88
import { SecuritySchemes } from '../../../src/models/v2/security-schemes';
99
import { Servers } from '../../../src/models/v2/servers';
10+
import { Collection } from '../../../src/models';
1011

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

@@ -189,6 +190,8 @@ describe('AsyncAPIDocument model', function() {
189190
const doc = serializeInput<v2.AsyncAPIObject>({ servers: { development: {} } });
190191
const d = new AsyncAPIDocument(doc);
191192
expect(d.allServers()).toBeInstanceOf(Servers);
193+
expect(d.allServers().all()).toBeInstanceOf(Array);
194+
expect(d.allServers().all()).not.toBeInstanceOf(Collection);
192195
expect(d.allServers()).toHaveLength(1);
193196
expect(d.allServers().all()[0].id()).toEqual('development');
194197
});
@@ -212,6 +215,8 @@ describe('AsyncAPIDocument model', function() {
212215
const doc = serializeInput<v2.AsyncAPIObject>({ channels: { 'user/signup': {} } });
213216
const d = new AsyncAPIDocument(doc);
214217
expect(d.allChannels()).toBeInstanceOf(Channels);
218+
expect(d.allChannels().all()).not.toBeInstanceOf(Collection);
219+
expect(d.allChannels().all()).toBeInstanceOf(Array);
215220
expect(d.allChannels()).toHaveLength(1);
216221
expect(d.allChannels().all()[0].address()).toEqual('user/signup');
217222
});
@@ -235,6 +240,8 @@ describe('AsyncAPIDocument model', function() {
235240
const doc = serializeInput<v2.AsyncAPIObject>({ channels: { 'user/signup': { publish: {}, subscribe: {} }, 'user/logout': { publish: {} } } });
236241
const d = new AsyncAPIDocument(doc);
237242
expect(d.allOperations()).toBeInstanceOf(Operations);
243+
expect(d.allOperations().all()).toBeInstanceOf(Array);
244+
expect(d.allOperations().all()).not.toBeInstanceOf(Collection);
238245
expect(d.allOperations()).toHaveLength(3);
239246
});
240247

@@ -257,6 +264,8 @@ describe('AsyncAPIDocument model', function() {
257264
it('should return a collection of messages', function() {
258265
const doc = serializeInput<v2.AsyncAPIObject>({ channels: { 'user/signup': { publish: { message: {} }, subscribe: { message: { oneOf: [{}, {}] } } }, 'user/logout': { publish: { message: {} } } } });
259266
const d = new AsyncAPIDocument(doc);
267+
expect(d.allMessages().all()).toBeInstanceOf(Array);
268+
expect(d.allMessages().all()).not.toBeInstanceOf(Collection);
260269
expect(d.allMessages()).toBeInstanceOf(Messages);
261270
expect(d.allMessages()).toHaveLength(4);
262271
});
@@ -282,6 +291,8 @@ describe('AsyncAPIDocument model', function() {
282291
const doc = serializeInput<v2.AsyncAPIObject>({ channels: { 'user/signup': { publish: { message: { payload: {} } }, subscribe: { message: { oneOf: [{ payload: {} }, {}, { payload: {} }] } } }, 'user/logout': { publish: { message: { payload: {} } } } } });
283292
const d = new AsyncAPIDocument(doc);
284293
expect(d.allSchemas()).toBeInstanceOf(Schemas);
294+
expect(d.allSchemas().all()).toBeInstanceOf(Array);
295+
expect(d.allSchemas().all()).not.toBeInstanceOf(Collection);
285296
expect(d.allSchemas()).toHaveLength(4);
286297
});
287298

test/models/v3/asyncapi.spec.ts

+125
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Servers } from '../../../src/models/v3/servers';
1111
import { serializeInput, assertExtensions } from './utils';
1212

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

1516
describe('AsyncAPIDocument model', function() {
1617
describe('.version()', function() {
@@ -164,6 +165,130 @@ describe('AsyncAPIDocument model', function() {
164165
});
165166
});
166167

168+
describe('.allMessages()', function() {
169+
it('should return a collection of messages', function() {
170+
const duplicatedMessage = {};
171+
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { userSignup: { address: 'user/signup', messages: { someMessage1: {}, someMessage2: duplicatedMessage } }, userLogout: { address: 'user/logout', messages: { someMessage3: duplicatedMessage } } } });
172+
const d = new AsyncAPIDocument(doc);
173+
expect(d.allMessages().all()).toBeInstanceOf(Array);
174+
expect(d.allMessages().all()).not.toBeInstanceOf(Collection);
175+
expect(d.allMessages()).toBeInstanceOf(Messages);
176+
expect(d.allMessages()).toHaveLength(2);
177+
});
178+
179+
it('should return all messages (with messages from components)', function() {
180+
const duplicatedMessage = {};
181+
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: {} } }});
182+
const d = new AsyncAPIDocument(doc);
183+
expect(d.allMessages()).toBeInstanceOf(Messages);
184+
expect(d.allMessages()).toHaveLength(4);
185+
});
186+
187+
it('should return a collection of messages even if messages are not defined', function() {
188+
const doc = serializeInput<v3.AsyncAPIObject>({});
189+
const d = new AsyncAPIDocument(doc);
190+
expect(d.allMessages()).toBeInstanceOf(Messages);
191+
});
192+
});
193+
194+
describe('.allSchemas()', function() {
195+
it('should return a collection of schemas', function() {
196+
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { userSignup: { address: 'user/signup', messages: { someMessage1: { payload: {}}, someMessage2: { payload: {} } } }, userLogout: { address: 'user/logout', messages: { someMessage3WithoutPayload: {} } } } });
197+
const d = new AsyncAPIDocument(doc);
198+
expect(d.allSchemas()).toBeInstanceOf(Schemas);
199+
expect(d.allSchemas().all()).toBeInstanceOf(Array);
200+
expect(d.allSchemas().all()).not.toBeInstanceOf(Collection);
201+
expect(d.allSchemas()).toHaveLength(2);
202+
});
203+
204+
it('should return all schemas (with schemas from components)', function() {
205+
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { userSignup: { address: 'user/signup', messages: { someMessage1: { payload: {}}, someMessage2: { payload: {} } } } }, components: { schemas: { schemaOne: {}, schemaTwo: {} } } });
206+
const d = new AsyncAPIDocument(doc);
207+
expect(d.allSchemas()).toBeInstanceOf(Schemas);
208+
expect(d.allSchemas()).toHaveLength(4);
209+
});
210+
211+
it('should return a collection of schemas even if collection is empty', function() {
212+
const doc = serializeInput<v3.AsyncAPIObject>({});
213+
const d = new AsyncAPIDocument(doc);
214+
expect(d.allSchemas()).toBeInstanceOf(Schemas);
215+
});
216+
});
217+
218+
describe('.allServers()', function() {
219+
it('should return a collection of servers', function() {
220+
const doc = serializeInput<v3.AsyncAPIObject>({ servers: { development: {} } });
221+
const d = new AsyncAPIDocument(doc);
222+
expect(d.allServers()).toBeInstanceOf(Servers);
223+
expect(d.allServers().all()).toBeInstanceOf(Array);
224+
expect(d.allServers().all()).not.toBeInstanceOf(Collection);
225+
expect(d.allServers()).toHaveLength(1);
226+
expect(d.allServers().all()[0].id()).toEqual('development');
227+
});
228+
229+
it('should return all servers (with servers from components)', function() {
230+
const doc = serializeInput<v3.AsyncAPIObject>({ servers: { production: {} }, components: { servers: { development: {} } } });
231+
const d = new AsyncAPIDocument(doc);
232+
expect(d.allServers()).toBeInstanceOf(Servers);
233+
expect(d.allServers()).toHaveLength(2);
234+
});
235+
236+
it('should return a collection of servers even if servers are not defined', function() {
237+
const doc = serializeInput<v3.AsyncAPIObject>({});
238+
const d = new AsyncAPIDocument(doc);
239+
expect(d.allServers()).toBeInstanceOf(Servers);
240+
});
241+
});
242+
243+
describe('.allChannels()', function() {
244+
it('should return a collection of channels', function() {
245+
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { 'user/signup': {} } });
246+
const d = new AsyncAPIDocument(doc);
247+
expect(d.allChannels()).toBeInstanceOf(Channels);
248+
expect(d.allChannels().all()).not.toBeInstanceOf(Collection);
249+
expect(d.allChannels().all()).toBeInstanceOf(Array);
250+
expect(d.allChannels()).toHaveLength(1);
251+
});
252+
253+
it('should return all channels (with channels from components)', function() {
254+
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { 'user/signup': {} }, components: { channels: { someChannel1: {}, someChannel2: {} } } });
255+
const d = new AsyncAPIDocument(doc);
256+
expect(d.allChannels()).toBeInstanceOf(Channels);
257+
expect(d.allChannels()).toHaveLength(3);
258+
});
259+
260+
it('should return a collection of channels even if channels are not defined', function() {
261+
const doc = serializeInput<v3.AsyncAPIObject>({});
262+
const d = new AsyncAPIDocument(doc);
263+
expect(d.allChannels()).toBeInstanceOf(Channels);
264+
});
265+
});
266+
267+
describe('.allOperations()', function() {
268+
it('should return a collection of operations', function() {
269+
const doc = serializeInput<v3.AsyncAPIObject>({ operations: { userSignup: {}, userLogout: {} } });
270+
const d = new AsyncAPIDocument(doc);
271+
expect(d.allOperations()).toBeInstanceOf(Operations);
272+
expect(d.allOperations().all()).toBeInstanceOf(Array);
273+
expect(d.allOperations().all()).not.toBeInstanceOf(Collection);
274+
expect(d.allOperations()).toHaveLength(2);
275+
});
276+
277+
it('should return all operations (with operations from components)', function() {
278+
const duplicatedOperation = { };
279+
const doc = serializeInput<v3.AsyncAPIObject>({ operations: { userSignup: duplicatedOperation, userLogout: {} }, components: { operations: { someOperation1: duplicatedOperation, someOperation2: {} } } });
280+
const d = new AsyncAPIDocument(doc);
281+
expect(d.allOperations()).toBeInstanceOf(Operations);
282+
expect(d.allOperations()).toHaveLength(3);
283+
});
284+
285+
it('should return a collection of operations even if operations are not defined', function() {
286+
const doc = serializeInput<v3.AsyncAPIObject>({});
287+
const d = new AsyncAPIDocument(doc);
288+
expect(d.allOperations()).toBeInstanceOf(Operations);
289+
});
290+
});
291+
167292
describe('.components()', function() {
168293
it('should return a components model', function() {
169294
const doc = serializeInput<v3.AsyncAPIObject>({ components: {} });

0 commit comments

Comments
 (0)