Skip to content

Commit

Permalink
chore: sync to master (#615)
Browse files Browse the repository at this point in the history
* fix: shuffle extra props to generate, closes #601

* fix: rework recursive references, closes #584

* fix: rework depth, closes #592

* chore: add missing tests

* chore: pass json-path flag

* cleanup
  • Loading branch information
pateketrueke committed Nov 2, 2020
1 parent 971be18 commit 581aeb2
Show file tree
Hide file tree
Showing 11 changed files with 595 additions and 141 deletions.
1 change: 1 addition & 0 deletions bin/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const noColor = process.argv.indexOf('--no-color') !== -1;

// FIXME: enable flags...
jsf.option({
resolveJsonPath: argv.flags.resolveJsonPath,
alwaysFakeOptionals: argv.flags.alwaysFakeOptionals,
});

Expand Down
13 changes: 10 additions & 3 deletions src/lib/core/buildResolveSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ const buildResolveSchema = ({
refDepthMax,
refDepthMin,
}) => {
const recursiveUtil = {};
const seenRefs = {};

let depth = 0;
let lastRef;

const recursiveUtil = {};
recursiveUtil.resolveSchema = (sub, index, rootPath) => {
// prevent null sub from default/example null values to throw
if (sub === null || sub === undefined) {
Expand All @@ -36,26 +38,31 @@ const buildResolveSchema = ({
const maxDepth = Math.max(refDepthMin, refDepthMax) - 1;

// increasing depth only for repeated refs seems to be fixing #258
if (sub.$ref === '#' || (lastRef === sub.$ref && ++depth > maxDepth)) {
if (sub.$ref === '#' || seenRefs[sub.$ref] < 0 || (lastRef === sub.$ref && ++depth > maxDepth)) {
delete sub.$ref;
return sub;
}

if (typeof seenRefs[sub.$ref] === 'undefined') {
seenRefs[sub.$ref] = random.number(refDepthMin, refDepthMax) - 1;
}

lastRef = sub.$ref;

let ref;

if (sub.$ref.indexOf('#/') === -1) {
ref = refs[sub.$ref] || null;
} else {
ref = utils.getLocalRef(schema, sub.$ref) || null;
ref = utils.getLocalRef(schema, sub.$ref) || null;
}

if (typeof ref !== 'undefined') {
if (!ref && optionAPI('ignoreMissingRefs') !== true) {
throw new Error(`Reference not found: ${sub.$ref}`);
}

seenRefs[sub.$ref] -= 1;
utils.merge(sub, ref || {});
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function objectType(value, path, resolve, traverseCallback) {

// properties are read from right-to-left
const _limit = optionalsProbability !== null ? max : random.number(0, max);
const _props = requiredProperties.concat(extraProperties.slice(0, _limit));
const _props = requiredProperties.concat(random.shuffle(extraProperties).slice(0, _limit));
const _defns = [];

if (value.dependencies) {
Expand Down
2 changes: 2 additions & 0 deletions tests/schema/core/issues/issue-427.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
"set": {
"optionalsProbability": 0.6
},
"throwsSometimes": "Given sample does not match schema",
"repeat": 100,
"valid": true
}
]
Expand Down
148 changes: 23 additions & 125 deletions tests/schema/core/issues/issue-569.json
Original file line number Diff line number Diff line change
@@ -1,128 +1,26 @@
{
"description": "keep empty required objects",
[
{
"description": "Required properties that result in empty objects get removed",
"tests": [
{
"description": "should not remove empty objects defined with additionalProperties",
"schema": {
"type": "object",
"properties": {
"requiredProperty": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"required": [
"requiredProperty"
]
},
"valid": true
{
"description": "should eventually generate some props",
"schema": {
"additionalProperties": false,
"type": "object",
"properties": {
"things": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"required": [
"things"
]
},
{
"description": "should not remove empty arrays",
"schema": {
"type": "object",
"properties": {
"requiredProperty": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"requiredProperty"
]
},
"valid": true
},
{
"description": "should not remove empty objects",
"schema": {
"type": "object",
"properties": {
"requiredProperty": {
"type": "object",
"properties": {
"optionalProperty": {
"type": "string"
}
}
}
},
"required": [
"requiredProperty"
]
},
"valid": true
},
{
"description": "should not remove empty objects when required by oneOf but not by root schema",
"schema": {
"type": "object",
"required": [
"id"
],
"properties": {
"id": {
"type": "string"
}
},
"oneOf": [
{
"properties": {
"type": {
"type": "object",
"properties": {}
},
"name": {
"type": "string"
}
},
"required": [
"type",
"name"
]
}
]
},
"valid": true
},
{
"description": "should not remove empty objects described in additionalProperties",
"schema": {
"type": "object",
"minProperties": 1,
"additionalProperties": {
"required": [
"id"
],
"properties": {
"id": {
"type": "string"
}
},
"oneOf": [
{
"properties": {
"type": {
"type": "object",
"properties": {}
},
"name": {
"type": "string"
}
},
"required": [
"type",
"name"
]
}
]
}
},
"valid": true
}
"valid": true
}
]
}
}
]
44 changes: 44 additions & 0 deletions tests/schema/core/issues/issue-584.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"description": "Maximum call stack size & refDepthMax not work",
"tests": [
{
"description": "should create items recursively",
"schema": {
"$ref": "#/definitions/A",
"definitions": {
"A": {
"type": "object",
"additionalProperties": false,
"properties": {
"nodeA": {
"type": "array",
"items": {
"$ref": "#/definitions/A"
}
},
"nodeB": {
"$ref": "#/definitions/B"
}
}
},
"B": {
"type": "object",
"additionalProperties": false,
"properties": {
"nodeB": {
"type": "array",
"items": {
"$ref": "#/definitions/B"
}
}
}
}
}
},
"repeat": 50,
"valid": true
}
]
}
]

0 comments on commit 581aeb2

Please sign in to comment.