json-machete 0.7.12 → 0.7.13-alpha-4f5d4b595.0
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 +65 -10
- package/index.mjs +65 -10
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -22,7 +22,7 @@ async function visitJSONSchema(schema, visitorFn, { visitedSubschemaResultMap, p
|
|
|
22
22
|
visitedSubschemaResultMap: new WeakMap(),
|
|
23
23
|
path: '',
|
|
24
24
|
keepObjectRef: false,
|
|
25
|
-
onCircularReference: exports.OnCircularReference.IGNORE,
|
|
25
|
+
onCircularReference: process.env.DEBUG ? exports.OnCircularReference.WARN : exports.OnCircularReference.IGNORE,
|
|
26
26
|
}) {
|
|
27
27
|
if (typeof schema === 'object') {
|
|
28
28
|
if (!visitedSubschemaResultMap.has(schema)) {
|
|
@@ -303,7 +303,7 @@ async function compareJSONSchemas(oldSchema, newSchema) {
|
|
|
303
303
|
visitedSubschemaResultMap: new WeakMap(),
|
|
304
304
|
path: '',
|
|
305
305
|
keepObjectRef: true,
|
|
306
|
-
onCircularReference: exports.OnCircularReference.IGNORE,
|
|
306
|
+
onCircularReference: process.env.DEBUG ? exports.OnCircularReference.WARN : exports.OnCircularReference.IGNORE,
|
|
307
307
|
});
|
|
308
308
|
if (breakingChanges.length > 0) {
|
|
309
309
|
throw new utils.AggregateError(breakingChanges.map(breakingChange => new Error(breakingChange)), `Breaking changes are found:\n${breakingChanges.join('\n')}`);
|
|
@@ -364,6 +364,9 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
364
364
|
return refMap.get($ref);
|
|
365
365
|
}
|
|
366
366
|
else {
|
|
367
|
+
if (process.env.DEBUG) {
|
|
368
|
+
console.log(`Dereferencing `, obj);
|
|
369
|
+
}
|
|
367
370
|
const [externalRelativeFilePath, refPath] = $ref.split('#');
|
|
368
371
|
if (externalRelativeFilePath) {
|
|
369
372
|
const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
|
|
@@ -539,15 +542,55 @@ async function getDeduplicatedTitles(schema) {
|
|
|
539
542
|
visitedSubschemaResultMap: new WeakMap(),
|
|
540
543
|
path: '',
|
|
541
544
|
keepObjectRef: true,
|
|
542
|
-
onCircularReference: exports.OnCircularReference.IGNORE,
|
|
545
|
+
onCircularReference: process.env.DEBUG ? exports.OnCircularReference.WARN : exports.OnCircularReference.IGNORE,
|
|
543
546
|
});
|
|
544
547
|
return duplicatedTypeNames;
|
|
545
548
|
}
|
|
546
549
|
async function healJSONSchema(schema) {
|
|
547
550
|
const deduplicatedSchema = deduplicateJSONSchema(schema);
|
|
548
551
|
const duplicatedTypeNames = await getDeduplicatedTitles(deduplicatedSchema);
|
|
549
|
-
return visitJSONSchema(deduplicatedSchema, (subSchema, { path })
|
|
552
|
+
return visitJSONSchema(deduplicatedSchema, async function healSubschema(subSchema, { path }) {
|
|
550
553
|
if (typeof subSchema === 'object') {
|
|
554
|
+
if (process.env.DEBUG) {
|
|
555
|
+
console.log(`Healing ${path}`);
|
|
556
|
+
}
|
|
557
|
+
// We don't support following properties
|
|
558
|
+
delete subSchema.readOnly;
|
|
559
|
+
delete subSchema.writeOnly;
|
|
560
|
+
const keys = Object.keys(subSchema);
|
|
561
|
+
if (keys.length === 0) {
|
|
562
|
+
subSchema.type = 'object';
|
|
563
|
+
subSchema.additionalProperties = true;
|
|
564
|
+
}
|
|
565
|
+
if (typeof subSchema.additionalProperties === 'object') {
|
|
566
|
+
delete subSchema.additionalProperties.readOnly;
|
|
567
|
+
delete subSchema.additionalProperties.writeOnly;
|
|
568
|
+
if (Object.keys(subSchema.additionalProperties).length === 0) {
|
|
569
|
+
subSchema.additionalProperties = true;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (subSchema.allOf != null && subSchema.allOf.length === 1) {
|
|
573
|
+
const realSubschema = subSchema.allOf[0];
|
|
574
|
+
delete subSchema.allOf;
|
|
575
|
+
return realSubschema;
|
|
576
|
+
}
|
|
577
|
+
if (subSchema.anyOf != null && subSchema.anyOf.length === 1) {
|
|
578
|
+
const realSubschema = subSchema.anyOf[0];
|
|
579
|
+
delete subSchema.anyOf;
|
|
580
|
+
return realSubschema;
|
|
581
|
+
}
|
|
582
|
+
if (subSchema.oneOf != null && subSchema.oneOf.length === 1) {
|
|
583
|
+
const realSubschema = subSchema.oneOf[0];
|
|
584
|
+
delete subSchema.oneOf;
|
|
585
|
+
return realSubschema;
|
|
586
|
+
}
|
|
587
|
+
if (subSchema.description != null) {
|
|
588
|
+
subSchema.description = subSchema.description.trim();
|
|
589
|
+
if (keys.length === 1) {
|
|
590
|
+
subSchema.type = 'object';
|
|
591
|
+
subSchema.additionalProperties = true;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
551
594
|
// Some JSON Schemas use this broken pattern and refer the type using `items`
|
|
552
595
|
if (subSchema.type === 'object' && subSchema.items) {
|
|
553
596
|
const realSubschema = subSchema.items;
|
|
@@ -560,6 +603,9 @@ async function healJSONSchema(schema) {
|
|
|
560
603
|
if (duplicatedTypeNames.has(subSchema.title)) {
|
|
561
604
|
delete subSchema.title;
|
|
562
605
|
}
|
|
606
|
+
if (typeof subSchema.example === 'object' && !subSchema.type) {
|
|
607
|
+
subSchema.type = 'object';
|
|
608
|
+
}
|
|
563
609
|
// Try to find the type
|
|
564
610
|
if (!subSchema.type) {
|
|
565
611
|
// If required exists without properties
|
|
@@ -615,7 +661,7 @@ async function healJSONSchema(schema) {
|
|
|
615
661
|
}
|
|
616
662
|
}
|
|
617
663
|
// If it is an object type but no properties given while example is available
|
|
618
|
-
if (subSchema.type === 'object' && !subSchema.properties && subSchema.example) {
|
|
664
|
+
if (((subSchema.type === 'object' && !subSchema.properties) || !subSchema.type) && subSchema.example) {
|
|
619
665
|
const generatedSchema = toJsonSchema(subSchema.example, {
|
|
620
666
|
required: false,
|
|
621
667
|
objects: {
|
|
@@ -628,7 +674,9 @@ async function healJSONSchema(schema) {
|
|
|
628
674
|
mode: 'first',
|
|
629
675
|
},
|
|
630
676
|
});
|
|
631
|
-
|
|
677
|
+
const healedGeneratedSchema = await healJSONSchema(generatedSchema);
|
|
678
|
+
subSchema.type = asArray(healedGeneratedSchema.type)[0];
|
|
679
|
+
subSchema.properties = healedGeneratedSchema.properties;
|
|
632
680
|
// If type for properties is already given, use it
|
|
633
681
|
if (typeof subSchema.additionalProperties === 'object') {
|
|
634
682
|
for (const propertyName in subSchema.properties) {
|
|
@@ -680,8 +728,9 @@ async function healJSONSchema(schema) {
|
|
|
680
728
|
pathBasedName += '_';
|
|
681
729
|
}
|
|
682
730
|
}
|
|
683
|
-
if (subSchema.
|
|
684
|
-
|
|
731
|
+
if (subSchema.type === 'object' && subSchema.properties && Object.keys(subSchema.properties).length === 0) {
|
|
732
|
+
delete subSchema.properties;
|
|
733
|
+
subSchema.additionalProperties = true;
|
|
685
734
|
}
|
|
686
735
|
}
|
|
687
736
|
return subSchema;
|
|
@@ -689,14 +738,17 @@ async function healJSONSchema(schema) {
|
|
|
689
738
|
visitedSubschemaResultMap: new WeakMap(),
|
|
690
739
|
path: '',
|
|
691
740
|
keepObjectRef: true,
|
|
692
|
-
onCircularReference: exports.OnCircularReference.IGNORE,
|
|
741
|
+
onCircularReference: process.env.DEBUG ? exports.OnCircularReference.WARN : exports.OnCircularReference.IGNORE,
|
|
693
742
|
});
|
|
694
743
|
}
|
|
695
744
|
|
|
696
745
|
async function referenceJSONSchema(schema) {
|
|
697
746
|
const definitions = {};
|
|
698
|
-
const finalSchema = await visitJSONSchema(schema, subSchema => {
|
|
747
|
+
const finalSchema = await visitJSONSchema(schema, (subSchema, { path }) => {
|
|
699
748
|
if (typeof subSchema === 'object') {
|
|
749
|
+
if (process.env.DEBUG) {
|
|
750
|
+
console.log(`Referencing ${path}`);
|
|
751
|
+
}
|
|
700
752
|
// Remove $id refs
|
|
701
753
|
delete subSchema.$id;
|
|
702
754
|
if (subSchema.$ref) {
|
|
@@ -724,6 +776,9 @@ async function referenceJSONSchema(schema) {
|
|
|
724
776
|
};
|
|
725
777
|
}
|
|
726
778
|
}
|
|
779
|
+
else if (subSchema.type === 'object') {
|
|
780
|
+
console.warn(`${path} cannot be referenced because it has no title`);
|
|
781
|
+
}
|
|
727
782
|
}
|
|
728
783
|
return subSchema;
|
|
729
784
|
});
|
package/index.mjs
CHANGED
|
@@ -17,7 +17,7 @@ async function visitJSONSchema(schema, visitorFn, { visitedSubschemaResultMap, p
|
|
|
17
17
|
visitedSubschemaResultMap: new WeakMap(),
|
|
18
18
|
path: '',
|
|
19
19
|
keepObjectRef: false,
|
|
20
|
-
onCircularReference: OnCircularReference.IGNORE,
|
|
20
|
+
onCircularReference: process.env.DEBUG ? OnCircularReference.WARN : OnCircularReference.IGNORE,
|
|
21
21
|
}) {
|
|
22
22
|
if (typeof schema === 'object') {
|
|
23
23
|
if (!visitedSubschemaResultMap.has(schema)) {
|
|
@@ -298,7 +298,7 @@ async function compareJSONSchemas(oldSchema, newSchema) {
|
|
|
298
298
|
visitedSubschemaResultMap: new WeakMap(),
|
|
299
299
|
path: '',
|
|
300
300
|
keepObjectRef: true,
|
|
301
|
-
onCircularReference: OnCircularReference.IGNORE,
|
|
301
|
+
onCircularReference: process.env.DEBUG ? OnCircularReference.WARN : OnCircularReference.IGNORE,
|
|
302
302
|
});
|
|
303
303
|
if (breakingChanges.length > 0) {
|
|
304
304
|
throw new AggregateError(breakingChanges.map(breakingChange => new Error(breakingChange)), `Breaking changes are found:\n${breakingChanges.join('\n')}`);
|
|
@@ -359,6 +359,9 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
359
359
|
return refMap.get($ref);
|
|
360
360
|
}
|
|
361
361
|
else {
|
|
362
|
+
if (process.env.DEBUG) {
|
|
363
|
+
console.log(`Dereferencing `, obj);
|
|
364
|
+
}
|
|
362
365
|
const [externalRelativeFilePath, refPath] = $ref.split('#');
|
|
363
366
|
if (externalRelativeFilePath) {
|
|
364
367
|
const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
|
|
@@ -534,15 +537,55 @@ async function getDeduplicatedTitles(schema) {
|
|
|
534
537
|
visitedSubschemaResultMap: new WeakMap(),
|
|
535
538
|
path: '',
|
|
536
539
|
keepObjectRef: true,
|
|
537
|
-
onCircularReference: OnCircularReference.IGNORE,
|
|
540
|
+
onCircularReference: process.env.DEBUG ? OnCircularReference.WARN : OnCircularReference.IGNORE,
|
|
538
541
|
});
|
|
539
542
|
return duplicatedTypeNames;
|
|
540
543
|
}
|
|
541
544
|
async function healJSONSchema(schema) {
|
|
542
545
|
const deduplicatedSchema = deduplicateJSONSchema(schema);
|
|
543
546
|
const duplicatedTypeNames = await getDeduplicatedTitles(deduplicatedSchema);
|
|
544
|
-
return visitJSONSchema(deduplicatedSchema, (subSchema, { path })
|
|
547
|
+
return visitJSONSchema(deduplicatedSchema, async function healSubschema(subSchema, { path }) {
|
|
545
548
|
if (typeof subSchema === 'object') {
|
|
549
|
+
if (process.env.DEBUG) {
|
|
550
|
+
console.log(`Healing ${path}`);
|
|
551
|
+
}
|
|
552
|
+
// We don't support following properties
|
|
553
|
+
delete subSchema.readOnly;
|
|
554
|
+
delete subSchema.writeOnly;
|
|
555
|
+
const keys = Object.keys(subSchema);
|
|
556
|
+
if (keys.length === 0) {
|
|
557
|
+
subSchema.type = 'object';
|
|
558
|
+
subSchema.additionalProperties = true;
|
|
559
|
+
}
|
|
560
|
+
if (typeof subSchema.additionalProperties === 'object') {
|
|
561
|
+
delete subSchema.additionalProperties.readOnly;
|
|
562
|
+
delete subSchema.additionalProperties.writeOnly;
|
|
563
|
+
if (Object.keys(subSchema.additionalProperties).length === 0) {
|
|
564
|
+
subSchema.additionalProperties = true;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if (subSchema.allOf != null && subSchema.allOf.length === 1) {
|
|
568
|
+
const realSubschema = subSchema.allOf[0];
|
|
569
|
+
delete subSchema.allOf;
|
|
570
|
+
return realSubschema;
|
|
571
|
+
}
|
|
572
|
+
if (subSchema.anyOf != null && subSchema.anyOf.length === 1) {
|
|
573
|
+
const realSubschema = subSchema.anyOf[0];
|
|
574
|
+
delete subSchema.anyOf;
|
|
575
|
+
return realSubschema;
|
|
576
|
+
}
|
|
577
|
+
if (subSchema.oneOf != null && subSchema.oneOf.length === 1) {
|
|
578
|
+
const realSubschema = subSchema.oneOf[0];
|
|
579
|
+
delete subSchema.oneOf;
|
|
580
|
+
return realSubschema;
|
|
581
|
+
}
|
|
582
|
+
if (subSchema.description != null) {
|
|
583
|
+
subSchema.description = subSchema.description.trim();
|
|
584
|
+
if (keys.length === 1) {
|
|
585
|
+
subSchema.type = 'object';
|
|
586
|
+
subSchema.additionalProperties = true;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
546
589
|
// Some JSON Schemas use this broken pattern and refer the type using `items`
|
|
547
590
|
if (subSchema.type === 'object' && subSchema.items) {
|
|
548
591
|
const realSubschema = subSchema.items;
|
|
@@ -555,6 +598,9 @@ async function healJSONSchema(schema) {
|
|
|
555
598
|
if (duplicatedTypeNames.has(subSchema.title)) {
|
|
556
599
|
delete subSchema.title;
|
|
557
600
|
}
|
|
601
|
+
if (typeof subSchema.example === 'object' && !subSchema.type) {
|
|
602
|
+
subSchema.type = 'object';
|
|
603
|
+
}
|
|
558
604
|
// Try to find the type
|
|
559
605
|
if (!subSchema.type) {
|
|
560
606
|
// If required exists without properties
|
|
@@ -610,7 +656,7 @@ async function healJSONSchema(schema) {
|
|
|
610
656
|
}
|
|
611
657
|
}
|
|
612
658
|
// If it is an object type but no properties given while example is available
|
|
613
|
-
if (subSchema.type === 'object' && !subSchema.properties && subSchema.example) {
|
|
659
|
+
if (((subSchema.type === 'object' && !subSchema.properties) || !subSchema.type) && subSchema.example) {
|
|
614
660
|
const generatedSchema = toJsonSchema(subSchema.example, {
|
|
615
661
|
required: false,
|
|
616
662
|
objects: {
|
|
@@ -623,7 +669,9 @@ async function healJSONSchema(schema) {
|
|
|
623
669
|
mode: 'first',
|
|
624
670
|
},
|
|
625
671
|
});
|
|
626
|
-
|
|
672
|
+
const healedGeneratedSchema = await healJSONSchema(generatedSchema);
|
|
673
|
+
subSchema.type = asArray(healedGeneratedSchema.type)[0];
|
|
674
|
+
subSchema.properties = healedGeneratedSchema.properties;
|
|
627
675
|
// If type for properties is already given, use it
|
|
628
676
|
if (typeof subSchema.additionalProperties === 'object') {
|
|
629
677
|
for (const propertyName in subSchema.properties) {
|
|
@@ -675,8 +723,9 @@ async function healJSONSchema(schema) {
|
|
|
675
723
|
pathBasedName += '_';
|
|
676
724
|
}
|
|
677
725
|
}
|
|
678
|
-
if (subSchema.
|
|
679
|
-
|
|
726
|
+
if (subSchema.type === 'object' && subSchema.properties && Object.keys(subSchema.properties).length === 0) {
|
|
727
|
+
delete subSchema.properties;
|
|
728
|
+
subSchema.additionalProperties = true;
|
|
680
729
|
}
|
|
681
730
|
}
|
|
682
731
|
return subSchema;
|
|
@@ -684,14 +733,17 @@ async function healJSONSchema(schema) {
|
|
|
684
733
|
visitedSubschemaResultMap: new WeakMap(),
|
|
685
734
|
path: '',
|
|
686
735
|
keepObjectRef: true,
|
|
687
|
-
onCircularReference: OnCircularReference.IGNORE,
|
|
736
|
+
onCircularReference: process.env.DEBUG ? OnCircularReference.WARN : OnCircularReference.IGNORE,
|
|
688
737
|
});
|
|
689
738
|
}
|
|
690
739
|
|
|
691
740
|
async function referenceJSONSchema(schema) {
|
|
692
741
|
const definitions = {};
|
|
693
|
-
const finalSchema = await visitJSONSchema(schema, subSchema => {
|
|
742
|
+
const finalSchema = await visitJSONSchema(schema, (subSchema, { path }) => {
|
|
694
743
|
if (typeof subSchema === 'object') {
|
|
744
|
+
if (process.env.DEBUG) {
|
|
745
|
+
console.log(`Referencing ${path}`);
|
|
746
|
+
}
|
|
695
747
|
// Remove $id refs
|
|
696
748
|
delete subSchema.$id;
|
|
697
749
|
if (subSchema.$ref) {
|
|
@@ -719,6 +771,9 @@ async function referenceJSONSchema(schema) {
|
|
|
719
771
|
};
|
|
720
772
|
}
|
|
721
773
|
}
|
|
774
|
+
else if (subSchema.type === 'object') {
|
|
775
|
+
console.warn(`${path} cannot be referenced because it has no title`);
|
|
776
|
+
}
|
|
722
777
|
}
|
|
723
778
|
return subSchema;
|
|
724
779
|
});
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-machete",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.13-alpha-4f5d4b595.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@graphql-mesh/cross-helpers": "0.1.1",
|
|
7
|
-
"@graphql-mesh/utils": "0.
|
|
7
|
+
"@graphql-mesh/utils": "0.34.0-alpha-4f5d4b595.0",
|
|
8
8
|
"@graphql-tools/utils": "8.6.9",
|
|
9
9
|
"@json-schema-tools/meta-schema": "1.6.19",
|
|
10
10
|
"cross-undici-fetch": "0.3.0",
|