Skip to content

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

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

Closed
DannyvdSluijs opened this issue Feb 26, 2025 · 0 comments · Fixed by #791

Comments

@DannyvdSluijs
Copy link
Collaborator

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());
    }
}
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

Successfully merging a pull request may close this issue.

1 participant