Skip to content

Recursion Update for php-json-schema #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
10 changes: 6 additions & 4 deletions src/RefResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RefResolver
public $url;
/** @var null|RefResolver */
private $rootResolver;
private int $max_deep_nesting = 200; //Change this via options submitted to ::import if needed

/**
* @param mixed $resolutionScope
Expand Down Expand Up @@ -102,9 +103,10 @@ public function setupResolutionScope($id, $data)
* RefResolver constructor.
* @param JsonSchema $rootData
*/
public function __construct($rootData = null)
public function __construct($rootData = null, int $max_nest_level = 200)
{
$this->rootData = $rootData;
$this->max_deep_nesting = $max_nest_level;
}

public function setRootData($rootData)
Expand Down Expand Up @@ -224,8 +226,8 @@ public function resolveReference($referencePath)
*/
public function preProcessReferences($data, Context $options, $nestingLevel = 0)
{
if ($nestingLevel > 200) {
throw new Exception('Too deep nesting level', Exception::DEEP_NESTING);
if ($nestingLevel > $this->max_deep_nesting) { //Updated due to specific recursion depth from Amazon product JSON Schemas - yep 200 was not enough
throw new Exception('Too deep nesting level. Suggest submitting maxNestLevel via options', Exception::DEEP_NESTING);
}
if (is_array($data)) {
foreach ($data as $key => $item) {
Expand Down Expand Up @@ -264,4 +266,4 @@ public function preProcessReferences($data, Context $options, $nestingLevel = 0)
}


}
}
2 changes: 1 addition & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function in($data, ?Context $options = null)
$options->import = true;

if ($options->refResolver === null) {
$options->refResolver = new RefResolver($data);
$options->refResolver = new RefResolver($data, $options->maxNestLevel ?? 200);
} else {
$options->refResolver->setRootData($data);
}
Expand Down