Skip to content

oneOf alters input value affecting validation on subsequent subschemas when type coercion is enabled #790

Closed
@DannyvdSluijs

Description

@DannyvdSluijs

As pointed out by @madman-81 in #770 and confirmed in #770 (comment) the oneOf alters the data when type coercion is enabled which ultimatly is correct. However the altered version of $value is passed to the subsequent subschemas that are part of the oneOf this can cause failing validation while with the original inputted $value it might validate.

This can be reproduced by the following:

<?php

namespace Tests\Unit\Validation;

use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator;
use PHPUnit\Framework\TestCase;

class JsonValidateV6Test extends TestCase
{
    public function testJsonCompare()
    {
        $schema = <<<JSON
            {
                "title": "Location",
                "type": "object",
                "properties": {
                    "id": {
                        "type": "string"
                    },
                    "related_locations": {
                        "oneOf": [
                            {
                                "type": "null"
                            },
                            {
                                "type": "array",
                                "items": {
                                    "type": "object",
                                    "properties": {
                                        "latitude": {
                                            "type": "string"
                                        },
                                        "longitude": {
                                            "type": "string"
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        JSON;

        $json = <<<JSON
            {
                "id": "LOC1",
                "related_locations": [
                    {
                        "latitude": "51.047598",
                        "longitude": "3.729943"
                    }
                ]
            }
        JSON;

        $jsonObj = json_decode($json);
        $schemaObj = json_decode($schema);

        $jsonSchemaValidation = new Validator();
        $jsonSchemaValidation->validate($jsonObj, $schemaObj, Constraint::CHECK_MODE_COERCE_TYPES);

        $this->assertTrue($jsonSchemaValidation->isValid());
    }
}

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