Skip to content

Commit 5210cd3

Browse files
authored
Merge pull request #737 from postmanlabs/feature/collection-size-improvements
Reduced collection size by keeping maximum generated elements for array as 1 for larger schemas.
2 parents b9bf427 + f37c8aa commit 5210cd3

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

assets/json-schema-faker.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23585,6 +23585,8 @@ function extend() {
2358523585
data['requiredOnly'] = false;
2358623586
data['minItems'] = 0;
2358723587
data['maxItems'] = null;
23588+
data['defaultMinItems'] = 2;
23589+
data['defaultMaxItems'] = 2;
2358823590
data['maxLength'] = null;
2358923591
data['resolveJsonPath'] = false;
2359023592
data['reuseProperties'] = false;
@@ -24161,6 +24163,25 @@ function extend() {
2416124163
}
2416224164
var minItems = value.minItems;
2416324165
var maxItems = value.maxItems;
24166+
24167+
/**
24168+
* Json schema faker fakes exactly maxItems # of elements in array if present.
24169+
* Hence we're keeping maxItems as minimum and valid as possible for schema faking (to lessen faked items)
24170+
* Maximum allowed maxItems is set to 20, set by Json schema faker option.
24171+
*/
24172+
// Override minItems to defaultMinItems if no minItems present
24173+
if (typeof minItems !== 'number' && maxItems && maxItems >= optionAPI('defaultMinItems')) {
24174+
minItems = optionAPI('defaultMinItems');
24175+
}
24176+
24177+
// Override maxItems to minItems if minItems is available
24178+
if (typeof minItems === 'number' && minItems > 0) {
24179+
maxItems = minItems;
24180+
}
24181+
24182+
// If no maxItems is defined than override with defaultMaxItems
24183+
typeof maxItems !== 'number' && (maxItems = optionAPI('defaultMaxItems'));
24184+
2416424185
if (optionAPI('minItems') && minItems === undefined) {
2416524186
// fix boundaries
2416624187
minItems = !maxItems

libV2/schemaUtils.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ const schemaFaker = require('../assets/json-schema-faker'),
7272
object: '<object>'
7373
},
7474

75+
// Maximum size of schema till whch we generate 2 elements per array (50 KB)
76+
SCHEMA_SIZE_OPTIMIZATION_THRESHOLD = 50 * 1024,
77+
7578
PROPERTIES_TO_ASSIGN_ON_CASCADE = ['type', 'nullable', 'properties'],
7679
crypto = require('crypto'),
7780

@@ -591,28 +594,6 @@ let QUERYPARAM = 'query',
591594
}
592595
// If schema is of type array
593596
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-
616597
schema.items = resolveSchema(context, schema.items, stack, resolveFor, _.cloneDeep(seenRef));
617598
}
618599
// Any properties to ignored should not be available in schema
@@ -771,15 +752,23 @@ let QUERYPARAM = 'query',
771752

772753
fakeSchema = (context, schema, shouldGenerateFromExample = true) => {
773754
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),
775759
fakedSchema;
776760

761+
// unassign potentially larger string data after calculation as not required
762+
stringifiedSchema = null;
763+
777764
if (context.schemaFakerCache[key]) {
778765
return context.schemaFakerCache[key];
779766
}
780767

781768
schemaFaker.option({
782-
useExamplesValue: shouldGenerateFromExample
769+
useExamplesValue: shouldGenerateFromExample,
770+
defaultMinItems: restrictArrayItems ? 1 : 2,
771+
defaultMaxItems: restrictArrayItems ? 1 : 2
783772
});
784773

785774
fakedSchema = schemaFaker(schema, null, context.schemaValidationCache || {});

0 commit comments

Comments
 (0)