@@ -72,6 +72,9 @@ const schemaFaker = require('../assets/json-schema-faker'),
72
72
object : '<object>'
73
73
} ,
74
74
75
+ // Maximum size of schema till whch we generate 2 elements per array (50 KB)
76
+ SCHEMA_SIZE_OPTIMIZATION_THRESHOLD = 50 * 1024 ,
77
+
75
78
PROPERTIES_TO_ASSIGN_ON_CASCADE = [ 'type' , 'nullable' , 'properties' ] ,
76
79
crypto = require ( 'crypto' ) ,
77
80
@@ -591,28 +594,6 @@ let QUERYPARAM = 'query',
591
594
}
592
595
// If schema is of type array
593
596
else if ( concreteUtils . compareTypes ( schema . type , SCHEMA_TYPES . array ) && schema . items ) {
594
- /*
595
- For VALIDATION - keep minItems and maxItems properties defined by user in schema as is
596
- FOR CONVERSION -
597
- Json schema faker fakes exactly maxItems # of elements in array
598
- Hence keeping maxItems as minimum and valid as possible for schema faking (to lessen faked items)
599
- We have enforced limit to maxItems as 100, set by Json schema faker option
600
- */
601
- if ( resolveFor === CONVERSION ) {
602
- // Override minItems to default (2) if no minItems present
603
- if ( ! _ . has ( schema , 'minItems' ) && _ . has ( schema , 'maxItems' ) && schema . maxItems >= 2 ) {
604
- schema . minItems = 2 ;
605
- }
606
-
607
- // Override maxItems to minItems if minItems is available
608
- if ( _ . has ( schema , 'minItems' ) && schema . minItems > 0 ) {
609
- schema . maxItems = schema . minItems ;
610
- }
611
-
612
- // If no maxItems is defined than override with default (2)
613
- ! _ . has ( schema , 'maxItems' ) && ( schema . maxItems = 2 ) ;
614
- }
615
-
616
597
schema . items = resolveSchema ( context , schema . items , stack , resolveFor , _ . cloneDeep ( seenRef ) ) ;
617
598
}
618
599
// Any properties to ignored should not be available in schema
@@ -771,15 +752,23 @@ let QUERYPARAM = 'query',
771
752
772
753
fakeSchema = ( context , schema , shouldGenerateFromExample = true ) => {
773
754
try {
774
- let key = hash ( JSON . stringify ( schema ) ) ,
755
+ let stringifiedSchema = typeof schema === 'object' && ( JSON . stringify ( schema ) ) ,
756
+ key = hash ( stringifiedSchema ) ,
757
+ restrictArrayItems = typeof stringifiedSchema === 'string' &&
758
+ ( stringifiedSchema . length > SCHEMA_SIZE_OPTIMIZATION_THRESHOLD ) ,
775
759
fakedSchema ;
776
760
761
+ // unassign potentially larger string data after calculation as not required
762
+ stringifiedSchema = null ;
763
+
777
764
if ( context . schemaFakerCache [ key ] ) {
778
765
return context . schemaFakerCache [ key ] ;
779
766
}
780
767
781
768
schemaFaker . option ( {
782
- useExamplesValue : shouldGenerateFromExample
769
+ useExamplesValue : shouldGenerateFromExample ,
770
+ defaultMinItems : restrictArrayItems ? 1 : 2 ,
771
+ defaultMaxItems : restrictArrayItems ? 1 : 2
783
772
} ) ;
784
773
785
774
fakedSchema = schemaFaker ( schema , null , context . schemaValidationCache || { } ) ;
0 commit comments