@@ -676,35 +676,102 @@ function buildCode (location, code, laterCode, locationPath) {
676
676
code += `if (obj['${ requiredProperty } '] === undefined) throw new Error('"${ requiredProperty } " is required!')\n`
677
677
}
678
678
679
- if ( schema . allOf ) {
680
- const builtCode = buildCodeWithAllOfs ( location , code , laterCode , locationPath )
681
- code = builtCode . code
682
- laterCode = builtCode . laterCode
683
- }
684
-
685
679
return { code, laterCode }
686
680
}
687
681
688
- function buildCodeWithAllOfs ( location , code , laterCode , locationPath ) {
689
- if ( location . schema . allOf ) {
690
- location . schema . allOf . forEach ( ( ss ) => {
691
- const builtCode = buildCodeWithAllOfs ( mergeLocation ( location , { schema : ss } ) , code , laterCode , locationPath )
692
- code = builtCode . code
693
- laterCode = builtCode . laterCode
694
- } )
695
- } else {
696
- const builtCode = buildCode ( location , code , laterCode , locationPath )
682
+ function mergeAllOfSchema ( location , schema , mergedSchema ) {
683
+ for ( let allOfSchema of schema . allOf ) {
684
+ if ( allOfSchema . $ref ) {
685
+ allOfSchema = refFinder ( allOfSchema . $ref , mergeLocation ( location , { schema : allOfSchema } ) ) . schema
686
+ }
697
687
698
- code = builtCode . code
699
- laterCode = builtCode . laterCode
700
- }
688
+ let allOfSchemaType = allOfSchema . type
689
+ if ( allOfSchemaType === undefined ) {
690
+ allOfSchemaType = inferTypeByKeyword ( allOfSchema )
691
+ }
701
692
702
- return { code, laterCode }
693
+ if ( allOfSchemaType !== undefined ) {
694
+ if (
695
+ mergedSchema . type !== undefined &&
696
+ mergedSchema . type !== allOfSchemaType
697
+ ) {
698
+ throw new Error ( 'allOf schemas have different type values' )
699
+ }
700
+ mergedSchema . type = allOfSchemaType
701
+ }
702
+
703
+ if ( allOfSchema . format !== undefined ) {
704
+ if (
705
+ mergedSchema . format !== undefined &&
706
+ mergedSchema . format !== allOfSchema . format
707
+ ) {
708
+ throw new Error ( 'allOf schemas have different format values' )
709
+ }
710
+ mergedSchema . format = allOfSchema . format
711
+ }
712
+
713
+ if ( allOfSchema . nullable !== undefined ) {
714
+ if (
715
+ mergedSchema . nullable !== undefined &&
716
+ mergedSchema . nullable !== allOfSchema . nullable
717
+ ) {
718
+ throw new Error ( 'allOf schemas have different nullable values' )
719
+ }
720
+ mergedSchema . nullable = allOfSchema . nullable
721
+ }
722
+
723
+ if ( allOfSchema . properties !== undefined ) {
724
+ if ( mergedSchema . properties === undefined ) {
725
+ mergedSchema . properties = { }
726
+ }
727
+ Object . assign ( mergedSchema . properties , allOfSchema . properties )
728
+ }
729
+
730
+ if ( allOfSchema . additionalProperties !== undefined ) {
731
+ if ( mergedSchema . additionalProperties === undefined ) {
732
+ mergedSchema . additionalProperties = { }
733
+ }
734
+ Object . assign ( mergedSchema . additionalProperties , allOfSchema . additionalProperties )
735
+ }
736
+
737
+ if ( allOfSchema . patternProperties !== undefined ) {
738
+ if ( mergedSchema . patternProperties === undefined ) {
739
+ mergedSchema . patternProperties = { }
740
+ }
741
+ Object . assign ( mergedSchema . patternProperties , allOfSchema . patternProperties )
742
+ }
743
+
744
+ if ( allOfSchema . required !== undefined ) {
745
+ if ( mergedSchema . required === undefined ) {
746
+ mergedSchema . required = [ ]
747
+ }
748
+ mergedSchema . required . push ( ...allOfSchema . required )
749
+ }
750
+
751
+ if ( allOfSchema . oneOf !== undefined ) {
752
+ if ( mergedSchema . oneOf === undefined ) {
753
+ mergedSchema . oneOf = [ ]
754
+ }
755
+ mergedSchema . oneOf . push ( ...allOfSchema . oneOf )
756
+ }
757
+
758
+ if ( allOfSchema . anyOf !== undefined ) {
759
+ if ( mergedSchema . anyOf === undefined ) {
760
+ mergedSchema . anyOf = [ ]
761
+ }
762
+ mergedSchema . anyOf . push ( ...allOfSchema . anyOf )
763
+ }
764
+
765
+ if ( allOfSchema . allOf !== undefined ) {
766
+ mergeAllOfSchema ( location , allOfSchema , mergedSchema )
767
+ }
768
+ }
769
+ delete mergedSchema . allOf
703
770
}
704
771
705
772
function buildInnerObject ( location , locationPath ) {
706
773
const schema = location . schema
707
- const result = buildCodeWithAllOfs ( location , '' , '' , locationPath )
774
+ const result = buildCode ( location , '' , '' , locationPath )
708
775
if ( schema . patternProperties ) {
709
776
const { code, laterCode } = addPatternProperties ( location )
710
777
result . code += code
@@ -1030,6 +1097,13 @@ function buildValue (laterCode, locationPath, input, location) {
1030
1097
}
1031
1098
}
1032
1099
1100
+ if ( schema . allOf ) {
1101
+ const mergedSchema = clone ( schema )
1102
+ mergeAllOfSchema ( location , schema , mergedSchema )
1103
+ schema = mergedSchema
1104
+ location . schema = mergedSchema
1105
+ }
1106
+
1033
1107
const type = schema . type
1034
1108
const nullable = schema . nullable === true
1035
1109
0 commit comments