-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy paththird-version.ts
611 lines (550 loc) · 21.1 KB
/
third-version.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import { isPlainObject, createRefObject, isRefObject, sortObjectKeys, getValueByRef, getValueByPath, createRefPath } from './utils';
import type { AsyncAPIDocument, ConvertOptions, ConvertV2ToV3Options, ConvertFunction } from './interfaces';
export const converters: Record<string, ConvertFunction> = {
'3.0.0': from__2_6_0__to__3_0_0,
}
type RequiredConvertV2ToV3Options = Required<ConvertV2ToV3Options>;
type ConvertContext = {
refs: Map<string, string>;
};
function from__2_6_0__to__3_0_0(asyncapi: AsyncAPIDocument, options: ConvertOptions): AsyncAPIDocument {
asyncapi.asyncapi = '3.0.0';
const v2tov3Options: RequiredConvertV2ToV3Options = {
pointOfView: 'application',
useChannelIdExtension: true,
convertServerComponents: true,
convertChannelComponents: true,
...(options.v2tov3 ?? {}),
} as RequiredConvertV2ToV3Options;
v2tov3Options.idGenerator = v2tov3Options.idGenerator || idGeneratorFactory(v2tov3Options);
const context: ConvertContext = {
refs: new Map(),
}
convertInfoObject(asyncapi, context);
if (isPlainObject(asyncapi.servers)) {
asyncapi.servers = convertServerObjects(asyncapi.servers, asyncapi);
}
if (isPlainObject(asyncapi.channels)) {
asyncapi.channels = convertChannelObjects(asyncapi.channels, asyncapi, v2tov3Options, context);
}
convertComponents(asyncapi, v2tov3Options, context);
replaceDeepRefs(asyncapi, context.refs, '', asyncapi);
return sortObjectKeys(
asyncapi,
['asyncapi', 'id', 'info', 'defaultContentType', 'servers', 'channels', 'operations', 'components']
);
}
/**
* Moving Tags and ExternalDocs to the Info Object.
*/
function convertInfoObject(asyncapi: AsyncAPIDocument, context: ConvertContext) {
if (asyncapi.tags) {
asyncapi.info.tags = asyncapi.tags;
context.refs.set(createRefPath('tags'), createRefPath('info', 'tags'));
delete asyncapi.tags;
}
if (asyncapi.externalDocs) {
asyncapi.info.externalDocs = asyncapi.externalDocs;
context.refs.set(createRefPath('externalDocs'), createRefPath('info', 'externalDocs'));
delete asyncapi.externalDocs;
}
asyncapi.info = sortObjectKeys(
asyncapi.info,
['title', 'version', 'description', 'termsOfService', 'contact', 'license', 'tags', 'externalDocs'],
);
}
/**
* Split `url` field to the `host` and `pathname` (optional) fields.
* Unify referencing mechanism in security field.
*/
function convertServerObjects(servers: Record<string, any>, asyncapi: AsyncAPIDocument) {
const newServers: Record<string, any> = {};
Object.entries(servers).forEach(([serverName, server]: [string, any]) => {
if (isRefObject(server)) {
newServers[serverName] = server;
return;
}
const { host, pathname, protocol } = resolveServerUrl(server.url);
server.host = host;
if (pathname !== undefined) {
server.pathname = pathname;
}
// Dont overwrite anything
if(protocol !== undefined && server.protocol === undefined) {
server.protocol = protocol;
}
delete server.url;
if (server.security) {
server.security = convertSecurityObject(server.security, asyncapi);
}
newServers[serverName] = sortObjectKeys(
server,
['host', 'pathname', 'protocol', 'protocolVersion', 'title', 'summary', 'description', 'variables', 'security', 'tags', 'externalDocs', 'bindings'],
);
});
return newServers;
}
type ToStandaloneOperationData = {
kind: 'subscribe' | 'publish',
channel: any,
asyncapi: any,
operations: any,
context:any,
inComponents: boolean,
channelId: string,
channelAddress: string,
options: any,
oldPath: string[]
}
/**
* Convert operation part of channels into standalone operations
*/
function toStandaloneOperation(data: ToStandaloneOperationData): Record<string, any> {
const {kind, channel, asyncapi, operations, context, inComponents, channelId, channelAddress, options, oldPath} = data;
let operation = channel[kind];
const operationPath = inComponents ? ['components', 'operations'] : ['operations'];
if (isPlainObject(operation)) {
const { operationId, operation: newOperation, messages } = convertOperationObject({ asyncapi, kind, channel, channelId, oldChannelId: channelAddress, operation, inComponents }, options, context);
if (operation.security) {
newOperation.security = convertSecurityObject(operation.security, asyncapi);
}
operationPath.push(operationId);
context.refs.set(createRefPath(...oldPath, kind), createRefPath(...operationPath));
operations[operationId] = newOperation;
delete channel[kind];
return messages ?? {};
}
return {};
}
/**
* Split Channel Objects to the Channel Objects and Operation Objects.
*/
function convertChannelObjects(channels: Record<string, any>, asyncapi: AsyncAPIDocument, options: RequiredConvertV2ToV3Options, context: ConvertContext, inComponents: boolean = false) {
const newChannels: Record<string, any> = {};
Object.entries(channels).forEach(([channelAddress, channel]) => {
const oldPath = inComponents ? ['components', 'channels', channelAddress] : ['channels', channelAddress];
const channelId = options.idGenerator({ asyncapi, kind: 'channel', key: channelAddress, path: oldPath, object: channel });
const newPath = inComponents ? ['components', 'channels', channelId] : ['channels', channelId];
context.refs.set(createRefPath(...oldPath), createRefPath(...newPath));
if (isRefObject(channel)) {
newChannels[channelId] = channel;
return;
}
// assign address
channel.address = channelAddress;
// change the Server names to the Refs
const servers = channel.servers;
if (Array.isArray(servers)) {
channel.servers = servers.map((serverName: string) => createRefObject('servers', serverName));
}
//Change parameter formats
if (isPlainObject(channel.parameters)) {
channel.parameters = convertParameters(channel.parameters);
}
const operations: Record<string, any> = {};
// serialize publish and subscribe Operation Objects to standalone object
const publishMessages = toStandaloneOperation({kind: 'publish', channel, asyncapi, operations, context, inComponents, channelId, channelAddress, options, oldPath});
const subscribeMessages = toStandaloneOperation({kind: 'subscribe', channel, asyncapi, operations, context, inComponents, channelId, channelAddress, options, oldPath});
if (publishMessages || subscribeMessages) {
const allOperationMessages = {
...publishMessages,
...subscribeMessages,
}
channel.messages = convertMessages({
messages: allOperationMessages
});
}
setOperationsOnRoot({operations, inComponents, asyncapi, oldPath});
newChannels[channelId] = sortObjectKeys(
channel,
['address', 'messages', 'title', 'summary', 'description', 'servers', 'parameters', 'tags', 'externalDocs', 'bindings'],
);
});
return newChannels;
}
type SetOperationsOnRootData = {
operations: any,
inComponents: boolean,
asyncapi: AsyncAPIDocument,
oldPath: string[]
}
/**
* Assign the operations to the root AsyncAPI object.
*/
function setOperationsOnRoot(data: SetOperationsOnRootData){
const {operations, inComponents, asyncapi, oldPath} = data;
if (Object.keys(operations)) {
if (inComponents) {
const components = asyncapi.components = asyncapi.components ?? {};
components.operations = { ...components.operations ?? {}, ...operations };
// if given component is used in the `channels` object then create references for operations in the `operations` object
if (channelIsUsed(asyncapi.channels ?? {}, oldPath)) {
const referencedOperations = Object.keys(operations).reduce((acc, current) => {
acc[current] = createRefObject('components', 'operations', current);
return acc;
}, {} as Record<string, any>);
asyncapi.operations = { ...asyncapi.operations ?? {}, ...referencedOperations };
}
} else {
asyncapi.operations = { ...asyncapi.operations ?? {}, ...operations };
}
}
}
type ConvertOperationObjectData = {
asyncapi: AsyncAPIDocument;
operation: any;
channel: any;
channelId: string;
oldChannelId: string;
kind: 'publish' | 'subscribe';
inComponents: boolean;
}
/**
* Points to the connected channel and split messages for channel
*/
function convertOperationObject(data: ConvertOperationObjectData, options: RequiredConvertV2ToV3Options, context: ConvertContext): { operationId: string, operation: any, messages?: Record<string, any> } {
const { asyncapi, channelId, oldChannelId, kind, inComponents } = data;
const operation = { ...data.operation };
const oldChannelPath = ['channels', oldChannelId];
if (inComponents) {
oldChannelPath.unshift('components');
}
const newChannelPath = ['channels', channelId];
if (inComponents) {
newChannelPath.unshift('components');
}
const operationId = options.idGenerator({ asyncapi, kind: 'operation', key: kind, path: oldChannelPath, object: data.operation, parentId: channelId });
operation.channel = createRefObject(...newChannelPath);
try {
delete operation.operationId;
} catch(err) {}
const isPublish = kind === 'publish';
if (options.pointOfView === 'application') {
operation.action = isPublish ? 'receive' : 'send';
} else {
operation.action = isPublish ? 'send' : 'receive';
}
const message = operation.message;
let serializedMessages: Record<string, any> = {};
if (message) {
delete operation.message;
const oldMessagePath = ['channels', oldChannelId, kind, 'message'];
const newMessagePath = ['channels', channelId, 'messages'];
if (inComponents) {
oldMessagePath.unshift('components');
newMessagePath.unshift('components');
}
serializedMessages = moveMessagesFromOperation(message, newMessagePath, oldMessagePath, asyncapi, options, context, operationId);
applyMessageRefsToOperation(serializedMessages, newMessagePath, operation);
}
const sortedOperation = sortObjectKeys(
operation,
['action', 'channel', 'title', 'summary', 'description', 'security', 'tags', 'externalDocs', 'bindings', 'traits'],
);
return { operationId, operation: sortedOperation, messages: serializedMessages };
}
/**
* Remove all messages under operations and return an object of them.
*/
function moveMessagesFromOperation(message: any, newMessagePath: string[], oldMessagePath: string[], asyncapi: any, options: any, context: any, operationId: string): Record<string, any> {
if (Array.isArray(message.oneOf)) {
//Message oneOf no longer exists, it's implicit by having multiple entires in the message object.
return message.oneOf.reduce((acc: Record<string, any>, current: any, index: number) => {
const messagePath = [...oldMessagePath, 'oneOf', index];
const messageId = options.idGenerator({ asyncapi, kind: 'message', key: index, path: messagePath, object: current, parentId: operationId });
context.refs.set(createRefPath(...messagePath), createRefPath(...newMessagePath, messageId));
acc[messageId] = current;
return acc;
}, {});
} else {
const messageId = options.idGenerator({ asyncapi, kind: 'message', key: 'message', path: oldMessagePath, object: message, parentId: operationId });
context.refs.set(createRefPath(...oldMessagePath), createRefPath(...newMessagePath, messageId));
return { [messageId]: message };
}
}
/**
* Add references of messages to operations.
*/
function applyMessageRefsToOperation(serializedMessages: Record<string, any>, newMessagePath: string[], operation: any) {
if (Object.keys(serializedMessages ?? {}).length > 0 ) {
const newOperationMessages: Array<any> = [];
Object.entries(serializedMessages).forEach(([messageId, messageValue]) => {
if (isRefObject(messageValue)) {
// shallow copy of JS reference
newOperationMessages.push({ ...messageValue });
} else {
const messagePath = [...newMessagePath, messageId];
newOperationMessages.push(createRefObject(...messagePath));
}
});
operation.messages = newOperationMessages;
}
}
type ConvertMessagesObjectData = {
messages: Record<string, any>
}
/**
* Convert messages that use custom schema format into schema union.
*/
function convertMessages(data: ConvertMessagesObjectData): Record<string, any>{
const messages = {...data.messages};
// Convert schema formats to union schemas
Object.entries(messages).forEach(([_, message]) => {
if(message.schemaFormat !== undefined) {
const payloadSchema = message.payload;
message.payload = {
schemaFormat: message.schemaFormat,
schema: payloadSchema
}
delete message.schemaFormat;
}
});
return messages;
}
/**
* Convert `channels`, `servers` and `securitySchemes` in components.
*/
function convertComponents(asyncapi: AsyncAPIDocument, options: RequiredConvertV2ToV3Options, context: ConvertContext) {
const components = asyncapi.components;
if (!isPlainObject(components)) {
return;
}
if (options.convertServerComponents && isPlainObject(components.servers)) {
components.servers = convertServerObjects(components.servers, asyncapi);
}
if (options.convertChannelComponents && isPlainObject(components.channels)) {
components.channels = convertChannelObjects(components.channels, asyncapi, options, context, true);
}
if (isPlainObject(components.securitySchemes)) {
components.securitySchemes = convertSecuritySchemes(components.securitySchemes);
}
if (isPlainObject(components.messages)) {
components.messages = convertMessages({
messages: components.messages
});
}
if (isPlainObject(components.parameters)) {
components.parameters = convertParameters(components.parameters);
}
}
/**
* Convert all parameters to the new v3 format
*/
function convertParameters(parameters: Record<string, any>): Record<string, any> {
const newParameters: Record<string, any> = {};
Object.entries(parameters).forEach(([name, parameter]) => {
newParameters[name] = convertParameter(parameter);
});
return newParameters;
}
/**
* Convert the old v2 parameter object to v3.
*
* Ensure that extensions and references are all kept as is.
*
* Does not include extensions from schema.
*/
function convertParameter(parameter: any): any {
const ref = parameter['$ref'] ?? null;
if(ref !== null) {
return {
$ref: ref
}
}
const enumValues = parameter.schema?.enum ?? null;
const defaultValues = parameter.schema?.default ?? null;
const description = parameter.description ?? parameter.schema?.description ?? null;
const examples = parameter.schema?.examples ?? null;
const location = parameter.location ?? null;
//Make sure we keep parameter extensions
const v2ParameterObjectProperties = ["location", "schema", "description"];
const v2ParameterObjectExtensions = Object.entries(parameter).filter(([key,]) => {
return !v2ParameterObjectProperties.includes(key);
});
//Return the new v3 parameter object
return Object.assign({...v2ParameterObjectExtensions},
enumValues === null ? null : {enum: enumValues},
defaultValues === null ? null : {default: defaultValues},
description === null ? null : {description},
examples === null ? null : {examples},
location === null ? null : {location}
);
}
/**
* Convert `channels`, `servers` and `securitySchemes` in components.
*/
function convertSecuritySchemes(securitySchemes: Record<string, any>): Record<string, any> {
const newSecuritySchemes: Record<string, any> = {};
Object.entries(securitySchemes).forEach(([name, scheme]) => {
newSecuritySchemes[name] = convertSecuritySchemeObject(scheme);
});
return newSecuritySchemes;
}
/**
* Unify referencing mechanism in security field
*/
function convertSecurityObject(security: Array<Record<string, Array<string>>>, asyncapi: AsyncAPIDocument) {
const newSecurity: Array<any> = [];
security.forEach(securityItem => {
Object.entries(securityItem).forEach(([securityName, scopes]) => {
// without scopes - use ref
if (!scopes.length) {
newSecurity.push(createRefObject('components', 'securitySchemes', securityName))
return;
}
// create new security scheme in the components/securitySchemes with appropriate scopes
const securityScheme = getValueByPath(asyncapi, ['components', 'securitySchemes', securityName]);
// handle logic only on `oauth2` and `openIdConnect` security mechanism
if (securityScheme.type === 'oauth2' || securityScheme.type === 'openIdConnect') {
const newSecurityScheme = convertSecuritySchemeObject(securityScheme);
newSecurity.push({
...newSecurityScheme,
scopes: [...scopes],
});
}
});
});
return newSecurity;
}
const flowKinds = ['implicit', 'password', 'clientCredentials', 'authorizationCode'];
/**
* Convert security scheme object to new from v3 version (flow.[x].scopes -> flow.[x].availableScopes).
*/
function convertSecuritySchemeObject(original: any) {
const securityScheme = JSON.parse(JSON.stringify(original));
if (securityScheme.flows) {
flowKinds.forEach(flow => {
const flowScheme = securityScheme.flows[flow];
if (flowScheme?.scopes) {
flowScheme.availableScopes = flowScheme.scopes;
delete flowScheme.scopes;
}
});
}
return securityScheme;
}
/**
* Split `url` to the `host` and `pathname` (optional) fields.
*
* This function takes care of https://github.com/asyncapi/spec/pull/888
*/
function resolveServerUrl(url: string): { host: string, pathname: string | undefined, protocol: string | undefined } {
let [maybeProtocol, maybeHost] = url.split('://');
if (!maybeHost) {
maybeHost = maybeProtocol;
}
const [host, ...pathnames] = maybeHost.split('/');
if (pathnames.length) {
return { host, pathname: `/${pathnames.join('/')}`, protocol: maybeProtocol };
}
return { host, pathname: undefined, protocol: maybeProtocol };
}
/**
* Check if given channel (based on path) is used in the `channels` object.
*/
function channelIsUsed(channels: Record<string, any>, path: Array<string | number>): boolean {
for (const channel of Object.values(channels)) {
if (isRefObject(channel) && createRefPath(...path) === channel.$ref) {
return true;
}
}
return false;
}
/**
* Replace all deep local references with the new beginning of ref (when object is moved to another place).
*/
function replaceDeepRefs(value: any, refs: ConvertContext['refs'], key: string | number, parent: any): void {
if (key === '$ref' && typeof value === 'string') {
const newRef = replaceRef(value, refs);
if (typeof newRef === 'string') {
parent[key] = newRef;
}
return;
}
if (Array.isArray(value)) {
return value.forEach((item, idx) => replaceDeepRefs(item, refs, idx, value));
}
if (value && typeof value === 'object') {
for (const objKey in value) {
replaceDeepRefs(value[objKey], refs, objKey, value);
}
}
}
function replaceRef(ref: string, refs: ConvertContext['refs']): string | undefined {
const allowed: string[] = [];
refs.forEach((_, key) => {
// few refs can be allowed
if (ref.startsWith(key)) {
allowed.push(key);
}
});
// find the longest one
allowed.sort((a, b) => a.length - b.length);
const from = allowed.pop();
if (!from) {
return;
}
const toReplace = refs.get(from);
if (toReplace) {
return ref.replace(from, toReplace);
}
}
/**
* Default function to generate ids for objects.
*/
function idGeneratorFactory(options: ConvertV2ToV3Options): ConvertV2ToV3Options['idGenerator'] {
const useChannelIdExtension = options.useChannelIdExtension;
return (data: Parameters<Exclude<ConvertV2ToV3Options['idGenerator'], undefined>>[0]): string => {
const { asyncapi, kind, object, key, parentId } = data;
switch (kind) {
case 'channel':
return generateIdForChannel(object, key, useChannelIdExtension);
case 'operation': {
const oldOperationId = object.operationId;
const operationId = oldOperationId || (parentId ? `${parentId}.${key}` : kind);
return operationId;
};
case 'message':
return generateIdForMessage(object, asyncapi, parentId, key);
default: return '';
}
};
}
function generateIdForChannel(object: any, key: any, useChannelIdExtension: boolean | undefined) {
if (isRefObject(object)) {
const id = key as string;
return id;
}
const channel = object;
let channelId: string;
if (useChannelIdExtension) {
channelId = channel['x-channelId'] || key as string;
} else {
channelId = key as string;
}
return channelId;
}
function generateIdForMessage(object: any, asyncapi: any, parentId: string | undefined, key: any) {
if (isRefObject(object)) {
const possibleMessage = getValueByRef(asyncapi, object.$ref);
if (possibleMessage?.messageId) {
const messageId = possibleMessage.messageId;
return messageId;
}
}
const messageId = object.messageId;
if (messageId) {
return messageId;
}
let operationKind: string;
const splitParentId = parentId!.split('.');
if (splitParentId.length === 1) {
operationKind = parentId as string;
} else {
operationKind = splitParentId.pop() as string;
}
if (typeof key === 'number') {
return `${operationKind}.message.${key}`;
}
return `${operationKind}.message`;
}