Skip to content
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

CHECK_MODE_APPLY_DEFAULTS uses defaults from first "oneOf"|"anyOf" option rather than the one that validates #510

Open
dkrieger opened this issue May 11, 2018 · 1 comment

Comments

@dkrieger
Copy link

Background

CHECK_MODE_APPLY_DEFAULTS uses defaults from first "oneOf" option rather than the one that validates. The same has been observed with "anyOf", but the test uses "oneOf"

Test

$schemaJson = <<<JSON
{
    "oneOf": [
        {
            "type": "object",
            "properties": {
                "foo": {
                    "type": "object",
                    "properties": {
                        "name": {"enum":["baz"],"default":"baz"},
                        "meta": {"enum":["baz"],"default":"baz"}
                    }
                }
            }
        },
        {
            "type": "object",
            "properties": {
                "foo": {
                    "type": "object",
                    "properties": {
                        "name": {"enum":["bar"],"default":"bar"},
                        "meta": {"enum":["bar"],"default":"bar"}
                    }
                }
            }
        },
        {
            "type": "object",
            "properties": {
                "foo": {
                    "type": "object",
                    "properties": {
                        "name": {"enum":["zip"],"default":"zip"},
                        "meta": {"enum":["zip"],"default":"zip"}
                    }
                }
            }
        }
    ]
}
JSON;

$dataJson = '{"foo": {"name": "bar"}}';
$data = json_decode($dataJson);

$validator = new \JsonSchema\Validator();
$validator->validate(
    $data,
    json_decode($schemaJson),
    \JsonSchema\Constraints\Constraint::CHECK_MODE_APPLY_DEFAULTS
);

echo json_encode($data, JSON_PRETTY_PRINT);

expected output

{
    "foo": {
        "name": "bar",
        "meta": "bar"
    }
}

actual output

{
    "foo": {
        "name": "bar",
        "meta": "baz"
    }
}

getErrors() output

print_r($validator->getErrors());

yields

Array
(
    [0] => Array
        (
            [property] => foo.name
            [pointer] => /foo/name
            [message] => Does not have a value in the enumeration ["zip"]
            [constraint] => enum
            [context] => 1
            [enum] => Array
                (
                    [0] => zip
                )

        )

    [1] => Array
        (
            [property] => foo.meta
            [pointer] => /foo/meta
            [message] => Does not have a value in the enumeration ["zip"]
            [constraint] => enum
            [context] => 1
            [enum] => Array
                (
                    [0] => zip
                )

        )

    [2] => Array
        (
            [property] => foo.name
            [pointer] => /foo/name
            [message] => Does not have a value in the enumeration ["baz"]
            [constraint] => enum
            [context] => 1
            [enum] => Array
                (
                    [0] => baz
                )

        )

    [3] => Array
        (
            [property] => foo.meta
            [pointer] => /foo/meta
            [message] => Does not have a value in the enumeration ["bar"]
            [constraint] => enum
            [context] => 1
            [enum] => Array
                (
                    [0] => bar
                )

        )

    [4] => Array
        (
            [property] =>
            [pointer] =>
            [message] => Failed to match exactly one schema
            [constraint] => oneOf
            [context] => 1
        )

)
@rodehoed
Copy link

This one has hit me very hard too :-) After a couple of hours I found this report and I can confirm the incorrect behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants