json-schema: infinite loop with Constraint::CHECK_MODE_APPLY_DEFAULTS

I’ve found an issue, with some schema, the validator will go into an infinite loop and eventually terminate when the allowed memory is exhausted, you can reproduce the issue with this code :

<?php

include_once __DIR__."/../vendor/autoload.php";

$validator = new \JsonSchema\Validator();

$data = [];
$schema = (object)['$ref' => 'http://json-schema.org/draft-04/schema'];

echo __LINE__, PHP_EOL;

$validator->validate($data, $schema);

echo __LINE__, PHP_EOL;

$validator->validate($data, $schema, \JsonSchema\Constraints\Constraint::CHECK_MODE_APPLY_DEFAULTS);

echo __LINE__, PHP_EOL;

I’m not sure yet what is causing this. I’ll guess a infinite recursion has been introduced in UndefinedConstraint::validateCommonProperties. I’ll try to make another reproduction of the bug with a smaller schema

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 37 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Following discussion on #263, I think that for now, not setting defaults-on-defaults is a good idea. Does anyone object to this? If not, then I will go ahead and implement this restriction.

@mathroc Have opened #365 to aid collaboration on this, so we’re at least looking at the same code.

Yeah, there’s some bloody weird behavior happening in there. Still haven’t found the root cause, although I’m getting there. Working on several things at once, so it’s taking a while.

Have nailed it down to this so far as a minimal test case:

{
    "properties": {
        "propertyOne": {
            "anyOf": [
                { "$ref": "#" }
            ],
            "default": {}
        }
    }
}