-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
641 lines (561 loc) · 18.3 KB
/
index.js
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
'use strict'
const { randomUUID } = require('node:crypto')
const { major: semverMajor } = require('semver')
const { RefResolver } = require('json-schema-ref-resolver')
const { compareObjectKeys } = require('./lib/utils.js')
const HTTP_METHODS = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace']
function compareJsonSchemas (
ctx,
sourceSchema,
targetSchema,
jsonPath,
derefJsonPath
) {
const changes = []
if (!sourceSchema && !targetSchema) {
return changes
}
if (ctx.changesCache[jsonPath]) {
return ctx.changesCache[jsonPath]
}
ctx.changesCache[jsonPath] = changes
const { sameKeys, addedKeys, removedKeys } = compareObjectKeys(sourceSchema, targetSchema)
for (const key of addedKeys) {
changes.push({
jsonPath: derefJsonPath + `/${key}`,
source: undefined,
target: targetSchema[key]
})
}
for (const key of removedKeys) {
changes.push({
jsonPath: derefJsonPath + `/${key}`,
source: sourceSchema[key],
target: undefined
})
}
for (const key of sameKeys) {
let sourceValue = sourceSchema[key]
let targetValue = targetSchema[key]
if (key === '$ref' && sourceValue === targetValue) {
jsonPath = sourceValue
sourceValue = ctx.sourceRefResolver.getSchema(ctx.sourceSchemaId, sourceValue)
targetValue = ctx.targetRefResolver.getSchema(ctx.targetSchemaId, targetValue)
}
if (sourceValue === targetValue) continue
if (
typeof sourceValue === 'object' &&
typeof targetValue === 'object' &&
sourceValue !== null &&
targetValue !== null
) {
const newJsonPath = key === '$ref' ? jsonPath : jsonPath + `/${key}`
const newDerefJsonPath = key === '$ref' ? derefJsonPath : derefJsonPath + `/${key}`
const keyChanges = compareJsonSchemas(
ctx,
sourceValue,
targetValue,
newJsonPath,
newDerefJsonPath,
changes
)
changes.push(...keyChanges)
} else {
changes.push({
jsonPath: derefJsonPath + `/${key}`,
source: sourceValue,
target: targetValue
})
}
}
return changes
}
function checkSchemaVersions (sourceSchemaVersion, targetSchemaVersion) {
if (typeof sourceSchemaVersion !== 'string') {
throw new TypeError('source schema version must be a string')
}
if (typeof targetSchemaVersion !== 'string') {
throw new TypeError('target schema version must be a string')
}
if (semverMajor(sourceSchemaVersion) !== semverMajor(targetSchemaVersion)) {
throw new Error('source and target schemas must have the same major version')
}
}
function compareParametersObjects (
ctx,
path,
method,
sourceParameterObjects,
targetParameterObjects
) {
const changes = []
sourceParameterObjects = sourceParameterObjects || []
targetParameterObjects = targetParameterObjects || []
for (const targetParameterObject of targetParameterObjects) {
const targetParameterName = targetParameterObject.name
const targetParameterIn = targetParameterObject.in
const sourceParameterObject = sourceParameterObjects.find(
parameterObject =>
parameterObject.name === targetParameterName &&
parameterObject.in === targetParameterIn
)
if (sourceParameterObject === undefined) {
changes.push({
type: 'parameter',
action: 'added',
name: targetParameterName,
in: targetParameterIn,
sourceSchema: undefined,
targetSchema: targetParameterObject,
comment: `${targetParameterIn} parameter "${targetParameterName}"` +
` has been added to ${method.toUpperCase()} "${path}" route`
})
continue
}
const paramChanges = []
const parametersSchemaChanges = compareJsonSchemas(
ctx,
sourceParameterObject.schema,
targetParameterObject.schema,
`#/paths${path}/${method}/parameters/${targetParameterName}`,
'#'
)
if (parametersSchemaChanges.length > 0) {
paramChanges.push({
keyword: 'schema',
changes: parametersSchemaChanges,
comment: 'parameter schema has been changed'
})
}
if (!sourceParameterObject.required && targetParameterObject.required) {
paramChanges.push({
keyword: 'required',
source: sourceParameterObject.required,
target: targetParameterObject.required,
comment: 'parameter has been made required'
})
}
if (paramChanges.length > 0) {
changes.push({
type: 'parameter',
action: 'changed',
name: targetParameterName,
in: targetParameterIn,
sourceSchema: sourceParameterObject,
targetSchema: targetParameterObject,
changes: paramChanges,
comment: `${targetParameterIn} parameter "${targetParameterName}"` +
` has been changed in ${method.toUpperCase()} "${path}" route`
})
}
}
for (const sourceParameterObject of sourceParameterObjects) {
const sourceParameterName = sourceParameterObject.name
const sourceParameterIn = sourceParameterObject.in
const targetParameterObject = targetParameterObjects.find(
parameterObject =>
parameterObject.name === sourceParameterName &&
parameterObject.in === sourceParameterIn
)
if (targetParameterObject === undefined) {
changes.push({
type: 'parameter',
action: 'deleted',
name: sourceParameterName,
in: sourceParameterIn,
sourceSchema: sourceParameterObject,
targetSchema: undefined,
comment: `${sourceParameterIn} parameter "${sourceParameterName}"` +
` has been deleted from ${method.toUpperCase()} "${path}" route`
})
continue
}
}
return changes
}
function compareRequestBodyObjects (
ctx,
path,
method,
sourceRequestBodyObject,
targetRequestBodyObject
) {
const changes = []
const sourceRequestBodyContent = sourceRequestBodyObject?.content || {}
const targetRequestBodyContent = targetRequestBodyObject?.content || {}
const { sameKeys, addedKeys, removedKeys } = compareObjectKeys(
sourceRequestBodyContent,
targetRequestBodyContent
)
for (const mediaType of addedKeys) {
const requestBodyObject = targetRequestBodyContent[mediaType]
changes.push({
type: 'requestBody',
action: 'added',
mediaType,
sourceSchema: undefined,
targetSchema: requestBodyObject,
comment: `request body for "${mediaType}" media type` +
` has been added to ${method.toUpperCase()} "${path}" route`
})
}
for (const mediaType of removedKeys) {
const requestBodyObject = sourceRequestBodyContent[mediaType]
changes.push({
type: 'requestBody',
action: 'deleted',
mediaType,
sourceSchema: requestBodyObject,
targetSchema: undefined,
comment: `request body for "${mediaType}" media type` +
` has been deleted from ${method.toUpperCase()} "${path}" route`
})
}
for (const mediaType of sameKeys) {
const sourceRequestBodyObject = sourceRequestBodyContent[mediaType]
const targetRequestBodyObject = targetRequestBodyContent[mediaType]
const requestBodyChanges = []
const requestBodySchemaChanges = compareJsonSchemas(
ctx,
sourceRequestBodyObject.schema,
targetRequestBodyObject.schema,
`#/paths${path}/${method}/requestBody/content/${mediaType}`,
'#'
)
if (requestBodySchemaChanges.length > 0) {
requestBodyChanges.push({
keyword: 'schema',
changes: requestBodySchemaChanges,
comment: 'request body schema has been changed'
})
}
if (!sourceRequestBodyObject.required && targetRequestBodyObject.required) {
requestBodyChanges.push({
keyword: 'required',
source: sourceRequestBodyObject.required,
target: targetRequestBodyObject.required,
comment: 'request body has been made required'
})
}
if (requestBodyChanges.length > 0) {
changes.push({
type: 'requestBody',
action: 'changed',
mediaType,
sourceSchema: sourceRequestBodyObject,
targetSchema: targetRequestBodyObject,
changes: requestBodyChanges,
comment: `request body for "${mediaType}" media type` +
` has been changed in ${method.toUpperCase()} "${path}" route`
})
}
}
return changes
}
function compareResponseObjects (
ctx,
path,
method,
sourceResponseObjects,
targetResponseObjects
) {
const changes = []
if (sourceResponseObjects == null && targetResponseObjects == null) {
return changes
}
for (const statusCode of Object.keys(targetResponseObjects || {})) {
const targetResponseObject = targetResponseObjects[statusCode]
const sourceResponseObject = sourceResponseObjects?.[statusCode]
for (const header of Object.keys(targetResponseObject.headers || {})) {
const targetHeaderObject = targetResponseObject.headers[header]
const sourceHeaderObject = sourceResponseObject?.headers?.[header]
if (!sourceHeaderObject) {
changes.push({
type: 'responseHeader',
action: 'added',
statusCode,
header,
sourceSchema: undefined,
targetSchema: targetHeaderObject,
comment: `response header for "${statusCode}" status code` +
` has been added to ${method.toUpperCase()} "${path}" route`
})
continue
}
const headerObjectChanges = []
const headerObjectSchemaChanges = compareJsonSchemas(
ctx,
sourceHeaderObject.schema,
targetHeaderObject.schema,
`#/paths${path}/${method}/responses/${statusCode}/headers/${header}`,
'#'
)
if (headerObjectSchemaChanges.length > 0) {
headerObjectChanges.push({
keyword: 'schema',
changes: headerObjectSchemaChanges,
comment: 'response header schema has been changed'
})
}
if (headerObjectChanges.length > 0) {
changes.push({
type: 'responseHeader',
action: 'changed',
statusCode,
header,
sourceSchema: sourceHeaderObject,
targetSchema: targetHeaderObject,
changes: headerObjectChanges,
comment: `response header for "${statusCode}" status code` +
` has been changed in ${method.toUpperCase()} "${path}" route`
})
}
}
for (const mediaType of Object.keys(targetResponseObject.content || {})) {
const targetMediaTypeObject = targetResponseObject.content[mediaType]
const sourceMediaTypeObject = sourceResponseObject?.content?.[mediaType]
if (!sourceMediaTypeObject) {
changes.push({
type: 'responseBody',
action: 'added',
statusCode,
mediaType,
sourceSchema: undefined,
targetSchema: targetMediaTypeObject,
comment: `response body for "${statusCode}" status code and "${mediaType}" media type` +
` has been added to ${method.toUpperCase()} "${path}" route`
})
continue
}
const responseBodyChanges = []
const mediaTypeSchemaChanges = compareJsonSchemas(
ctx,
sourceMediaTypeObject.schema,
targetMediaTypeObject.schema,
`#/paths${path}/${method}/responses/${statusCode}/content/${mediaType}`,
'#'
)
if (mediaTypeSchemaChanges.length > 0) {
responseBodyChanges.push({
keyword: 'schema',
changes: mediaTypeSchemaChanges,
comment: 'response body schema has been changed'
})
}
if (responseBodyChanges.length > 0) {
changes.push({
type: 'responseBody',
action: 'changed',
statusCode,
mediaType,
sourceSchema: sourceMediaTypeObject,
targetSchema: targetMediaTypeObject,
changes: responseBodyChanges,
comment: `response body for "${statusCode}" status code and "${mediaType}" media type` +
` has been changed in ${method.toUpperCase()} "${path}" route`
})
}
}
}
for (const statusCode of Object.keys(sourceResponseObjects || {})) {
const sourceResponseObject = sourceResponseObjects[statusCode]
const targetResponseObject = targetResponseObjects?.[statusCode]
for (const header of Object.keys(sourceResponseObject.headers || {})) {
const sourceHeaderObject = sourceResponseObject.headers[header]
const targetHeaderObject = targetResponseObject?.headers?.[header]
if (!targetHeaderObject) {
changes.push({
type: 'responseHeader',
action: 'deleted',
statusCode,
header,
sourceSchema: sourceHeaderObject,
targetSchema: undefined,
comment: `response header for "${statusCode}" status code` +
` has been deleted from ${method.toUpperCase()} "${path}" route`
})
continue
}
}
for (const mediaType of Object.keys(sourceResponseObject.content || {})) {
const sourceMediaTypeObject = sourceResponseObject.content[mediaType]
const targetMediaTypeObject = targetResponseObject?.content?.[mediaType]
if (!targetMediaTypeObject) {
changes.push({
type: 'responseBody',
action: 'deleted',
statusCode,
mediaType,
sourceSchema: sourceMediaTypeObject,
targetSchema: undefined,
comment: `response body for "${statusCode}" status code and "${mediaType}" media type` +
` has been deleted from ${method.toUpperCase()} "${path}" route`
})
continue
}
}
}
return changes
}
function compareOperationObjects (
ctx,
path,
method,
sourceOperationObject,
targetOperationObject
) {
const parameterObjectsChanges = compareParametersObjects(
ctx,
path,
method,
sourceOperationObject.parameters,
targetOperationObject.parameters
)
const requestBodyObjectsChanges = compareRequestBodyObjects(
ctx,
path,
method,
sourceOperationObject.requestBody,
targetOperationObject.requestBody
)
const responseObjectsChanges = compareResponseObjects(
ctx,
path,
method,
sourceOperationObject.responses,
targetOperationObject.responses
)
if (
parameterObjectsChanges.length === 0 &&
requestBodyObjectsChanges.length === 0 &&
responseObjectsChanges.length === 0
) {
ctx.sameOperations.push({
method,
path,
sourceSchema: sourceOperationObject,
targetSchema: targetOperationObject
})
return
}
ctx.changesOperations.push({
method,
path,
sourceSchema: sourceOperationObject,
targetSchema: targetOperationObject,
changes: [
...parameterObjectsChanges,
...requestBodyObjectsChanges,
...responseObjectsChanges
]
})
}
function comparePathObjects (ctx, path, sourcePathObject, targetPathObject) {
const { sameKeys, addedKeys, removedKeys } = compareObjectKeys(
sourcePathObject,
targetPathObject
)
for (let method of addedKeys) {
method = method.toLocaleLowerCase()
if (!HTTP_METHODS.includes(method)) continue
const targetOperationObject = targetPathObject[method]
ctx.addedOperations.push({
method,
path,
targetSchema: targetOperationObject
})
}
for (let method of removedKeys) {
method = method.toLocaleLowerCase()
if (!HTTP_METHODS.includes(method)) continue
const sourceOperationObject = sourcePathObject[method]
ctx.deletedOperations.push({
method,
path,
sourceSchema: sourceOperationObject
})
}
for (let method of sameKeys) {
method = method.toLocaleLowerCase()
if (!HTTP_METHODS.includes(method)) continue
// TODO: check for uppercase methods here
const sourceOperationObject = sourcePathObject[method]
const targetOperationObject = targetPathObject[method]
compareOperationObjects(ctx, path, method, sourceOperationObject, targetOperationObject)
}
}
function comparePathsObjects (ctx, sourcePathsObjects, targetPathsObjects) {
const { sameKeys, addedKeys, removedKeys } = compareObjectKeys(
sourcePathsObjects,
targetPathsObjects
)
for (const path of addedKeys) {
const pathSchema = targetPathsObjects[path]
// TODO: check for $ref here
for (let method of Object.keys(pathSchema)) {
method = method.toLocaleLowerCase()
if (!HTTP_METHODS.includes(method)) continue
const operationSchema = pathSchema[method]
ctx.addedOperations.push({
method,
path,
targetSchema: operationSchema
})
}
}
for (const path of removedKeys) {
const pathSchema = sourcePathsObjects[path]
// TODO: check for $ref here
for (let method in pathSchema) {
method = method.toLocaleLowerCase()
if (!HTTP_METHODS.includes(method)) continue
const operationSchema = pathSchema[method]
ctx.deletedOperations.push({
method,
path,
sourceSchema: operationSchema
})
}
}
for (const path of sameKeys) {
const sourcePathObject = sourcePathsObjects[path]
const targetPathObject = targetPathsObjects[path]
// TODO: check for $ref here
comparePathObjects(ctx, path, sourcePathObject, targetPathObject)
}
}
function compareOpenApiSchemas (sourceSchema, targetSchema) {
if (typeof sourceSchema !== 'object' || sourceSchema === null) {
throw new TypeError('source schema must be an object')
}
if (typeof targetSchema !== 'object' || targetSchema === null) {
throw new TypeError('target schema must be an object')
}
checkSchemaVersions(sourceSchema.openapi, targetSchema.openapi)
const ctx = {
changesCache: {},
sourceSchemaId: randomUUID(),
targetSchemaId: randomUUID(),
sourceRefResolver: new RefResolver(),
targetRefResolver: new RefResolver(),
sameOperations: [],
addedOperations: [],
deletedOperations: [],
changesOperations: []
}
ctx.sourceRefResolver.addSchema(sourceSchema, ctx.sourceSchemaId)
ctx.targetRefResolver.addSchema(targetSchema, ctx.targetSchemaId)
comparePathsObjects(ctx, sourceSchema.paths, targetSchema.paths)
const isEqual =
ctx.addedOperations.length === 0 &&
ctx.deletedOperations.length === 0 &&
ctx.changesOperations.length === 0
return {
isEqual,
sameRoutes: ctx.sameOperations,
addedRoutes: ctx.addedOperations,
deletedRoutes: ctx.deletedOperations,
changedRoutes: ctx.changesOperations
}
}
module.exports = compareOpenApiSchemas