Skip to content

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

Closed
@dkrieger

Description

@dkrieger

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
        )

)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions