json-machete 0.0.16-alpha-2b7536c4f.0 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -64
- package/index.mjs +2 -64
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -292,99 +292,37 @@ async function compareJSONSchemas(oldSchema, newSchema) {
|
|
|
292
292
|
}
|
|
293
293
|
|
|
294
294
|
const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
|
|
295
|
-
function deduplicateObject(object, seenMap = new Map()) {
|
|
296
|
-
if (typeof object === 'object' && object != null) {
|
|
297
|
-
const stringified = JSON.stringify(object);
|
|
298
|
-
const seen = seenMap.get(stringified);
|
|
299
|
-
if (seen) {
|
|
300
|
-
return seen;
|
|
301
|
-
}
|
|
302
|
-
seenMap.set(stringified, object);
|
|
303
|
-
for (const key in object) {
|
|
304
|
-
object[key] = deduplicateObject(object[key], seenMap);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
return object;
|
|
308
|
-
}
|
|
309
|
-
async function getDeduplicatedTitles(schema) {
|
|
310
|
-
const duplicatedTypeNames = new Set();
|
|
311
|
-
const seenTypeNames = new Set();
|
|
312
|
-
await visitJSONSchema(schema, subSchema => {
|
|
313
|
-
if (typeof subSchema === 'object' && subSchema.title) {
|
|
314
|
-
if (seenTypeNames.has(subSchema.title)) {
|
|
315
|
-
duplicatedTypeNames.add(subSchema.title);
|
|
316
|
-
}
|
|
317
|
-
else {
|
|
318
|
-
seenTypeNames.add(subSchema.title);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
return subSchema;
|
|
322
|
-
}, {
|
|
323
|
-
visitedSubschemaResultMap: new WeakMap(),
|
|
324
|
-
path: '',
|
|
325
|
-
keepObjectRef: true,
|
|
326
|
-
onCircularReference: exports.OnCircularReference.IGNORE,
|
|
327
|
-
});
|
|
328
|
-
return duplicatedTypeNames;
|
|
329
|
-
}
|
|
330
295
|
async function healJSONSchema(schema) {
|
|
331
|
-
|
|
332
|
-
const duplicatedTypeNames = await getDeduplicatedTitles(deduplicatedSchema);
|
|
333
|
-
return visitJSONSchema(deduplicatedSchema, (subSchema, { path }) => {
|
|
296
|
+
return visitJSONSchema(schema, (subSchema, { path }) => {
|
|
334
297
|
if (typeof subSchema === 'object') {
|
|
335
|
-
if (duplicatedTypeNames.has(subSchema.title)) {
|
|
336
|
-
delete subSchema.title;
|
|
337
|
-
}
|
|
338
298
|
if (!subSchema.title && !subSchema.$ref) {
|
|
339
|
-
// Try to get definition name
|
|
299
|
+
// Try to get definition name
|
|
340
300
|
const maybeDefinitionBasedPath = path.includes('/definitions/') ? path.split('/definitions/')[1] : path;
|
|
341
301
|
let pathBasedName = maybeDefinitionBasedPath.split('/properties').join('').split('/').join('_');
|
|
342
302
|
switch (subSchema.type) {
|
|
343
303
|
case 'string':
|
|
344
|
-
// If it has special pattern, use path based name because it is specific
|
|
345
304
|
if (subSchema.pattern || subSchema.maxLength || subSchema.minLength || subSchema.enum) {
|
|
346
305
|
subSchema.title = pathBasedName;
|
|
347
|
-
// Otherwise use the format name
|
|
348
306
|
}
|
|
349
307
|
else if (subSchema.format) {
|
|
350
308
|
subSchema.title = subSchema.format;
|
|
351
309
|
}
|
|
352
310
|
break;
|
|
353
311
|
case 'integer':
|
|
354
|
-
// Use format name
|
|
355
312
|
if (subSchema.format) {
|
|
356
313
|
subSchema.title = subSchema.format;
|
|
357
314
|
}
|
|
358
315
|
break;
|
|
359
316
|
}
|
|
360
|
-
// If type name is reserved, add a suffix
|
|
361
317
|
if (reservedTypeNames.includes(pathBasedName)) {
|
|
362
318
|
pathBasedName += '_';
|
|
363
319
|
}
|
|
364
320
|
subSchema.title = subSchema.title || pathBasedName;
|
|
365
321
|
}
|
|
366
|
-
// Try to find the type
|
|
367
322
|
if (!subSchema.type) {
|
|
368
|
-
// If required exists without properties
|
|
369
|
-
if (subSchema.required && !subSchema.properties) {
|
|
370
|
-
// Add properties
|
|
371
|
-
subSchema.properties = {};
|
|
372
|
-
for (const missingPropertyName of subSchema.required) {
|
|
373
|
-
subSchema.properties[missingPropertyName] = {
|
|
374
|
-
oneOf: [
|
|
375
|
-
{ type: 'string' },
|
|
376
|
-
{ type: 'integer' },
|
|
377
|
-
{ type: 'boolean' },
|
|
378
|
-
{ type: 'object', additionalProperties: true },
|
|
379
|
-
],
|
|
380
|
-
};
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
// Properties only exist in objects
|
|
384
323
|
if (subSchema.properties) {
|
|
385
324
|
subSchema.type = 'object';
|
|
386
325
|
}
|
|
387
|
-
// Items only exist in arrays
|
|
388
326
|
if (subSchema.items) {
|
|
389
327
|
subSchema.type = 'array';
|
|
390
328
|
}
|
package/index.mjs
CHANGED
|
@@ -289,99 +289,37 @@ async function compareJSONSchemas(oldSchema, newSchema) {
|
|
|
289
289
|
}
|
|
290
290
|
|
|
291
291
|
const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
|
|
292
|
-
function deduplicateObject(object, seenMap = new Map()) {
|
|
293
|
-
if (typeof object === 'object' && object != null) {
|
|
294
|
-
const stringified = JSON.stringify(object);
|
|
295
|
-
const seen = seenMap.get(stringified);
|
|
296
|
-
if (seen) {
|
|
297
|
-
return seen;
|
|
298
|
-
}
|
|
299
|
-
seenMap.set(stringified, object);
|
|
300
|
-
for (const key in object) {
|
|
301
|
-
object[key] = deduplicateObject(object[key], seenMap);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
return object;
|
|
305
|
-
}
|
|
306
|
-
async function getDeduplicatedTitles(schema) {
|
|
307
|
-
const duplicatedTypeNames = new Set();
|
|
308
|
-
const seenTypeNames = new Set();
|
|
309
|
-
await visitJSONSchema(schema, subSchema => {
|
|
310
|
-
if (typeof subSchema === 'object' && subSchema.title) {
|
|
311
|
-
if (seenTypeNames.has(subSchema.title)) {
|
|
312
|
-
duplicatedTypeNames.add(subSchema.title);
|
|
313
|
-
}
|
|
314
|
-
else {
|
|
315
|
-
seenTypeNames.add(subSchema.title);
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
return subSchema;
|
|
319
|
-
}, {
|
|
320
|
-
visitedSubschemaResultMap: new WeakMap(),
|
|
321
|
-
path: '',
|
|
322
|
-
keepObjectRef: true,
|
|
323
|
-
onCircularReference: exports.OnCircularReference.IGNORE,
|
|
324
|
-
});
|
|
325
|
-
return duplicatedTypeNames;
|
|
326
|
-
}
|
|
327
292
|
async function healJSONSchema(schema) {
|
|
328
|
-
|
|
329
|
-
const duplicatedTypeNames = await getDeduplicatedTitles(deduplicatedSchema);
|
|
330
|
-
return visitJSONSchema(deduplicatedSchema, (subSchema, { path }) => {
|
|
293
|
+
return visitJSONSchema(schema, (subSchema, { path }) => {
|
|
331
294
|
if (typeof subSchema === 'object') {
|
|
332
|
-
if (duplicatedTypeNames.has(subSchema.title)) {
|
|
333
|
-
delete subSchema.title;
|
|
334
|
-
}
|
|
335
295
|
if (!subSchema.title && !subSchema.$ref) {
|
|
336
|
-
// Try to get definition name
|
|
296
|
+
// Try to get definition name
|
|
337
297
|
const maybeDefinitionBasedPath = path.includes('/definitions/') ? path.split('/definitions/')[1] : path;
|
|
338
298
|
let pathBasedName = maybeDefinitionBasedPath.split('/properties').join('').split('/').join('_');
|
|
339
299
|
switch (subSchema.type) {
|
|
340
300
|
case 'string':
|
|
341
|
-
// If it has special pattern, use path based name because it is specific
|
|
342
301
|
if (subSchema.pattern || subSchema.maxLength || subSchema.minLength || subSchema.enum) {
|
|
343
302
|
subSchema.title = pathBasedName;
|
|
344
|
-
// Otherwise use the format name
|
|
345
303
|
}
|
|
346
304
|
else if (subSchema.format) {
|
|
347
305
|
subSchema.title = subSchema.format;
|
|
348
306
|
}
|
|
349
307
|
break;
|
|
350
308
|
case 'integer':
|
|
351
|
-
// Use format name
|
|
352
309
|
if (subSchema.format) {
|
|
353
310
|
subSchema.title = subSchema.format;
|
|
354
311
|
}
|
|
355
312
|
break;
|
|
356
313
|
}
|
|
357
|
-
// If type name is reserved, add a suffix
|
|
358
314
|
if (reservedTypeNames.includes(pathBasedName)) {
|
|
359
315
|
pathBasedName += '_';
|
|
360
316
|
}
|
|
361
317
|
subSchema.title = subSchema.title || pathBasedName;
|
|
362
318
|
}
|
|
363
|
-
// Try to find the type
|
|
364
319
|
if (!subSchema.type) {
|
|
365
|
-
// If required exists without properties
|
|
366
|
-
if (subSchema.required && !subSchema.properties) {
|
|
367
|
-
// Add properties
|
|
368
|
-
subSchema.properties = {};
|
|
369
|
-
for (const missingPropertyName of subSchema.required) {
|
|
370
|
-
subSchema.properties[missingPropertyName] = {
|
|
371
|
-
oneOf: [
|
|
372
|
-
{ type: 'string' },
|
|
373
|
-
{ type: 'integer' },
|
|
374
|
-
{ type: 'boolean' },
|
|
375
|
-
{ type: 'object', additionalProperties: true },
|
|
376
|
-
],
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
// Properties only exist in objects
|
|
381
320
|
if (subSchema.properties) {
|
|
382
321
|
subSchema.type = 'object';
|
|
383
322
|
}
|
|
384
|
-
// Items only exist in arrays
|
|
385
323
|
if (subSchema.items) {
|
|
386
324
|
subSchema.type = 'array';
|
|
387
325
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-machete",
|
|
3
|
-
"version": "0.0.16
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@graphql-mesh/utils": "0.17.1
|
|
6
|
+
"@graphql-mesh/utils": "0.17.1",
|
|
7
7
|
"@json-schema-tools/meta-schema": "1.6.19",
|
|
8
8
|
"json-ptr": "2.2.0"
|
|
9
9
|
},
|