Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Validator erroneously merges constraints from overriden properties #15950

Copy link
Copy link
Closed
@eggyal

Description

@eggyal
Issue body actions

A simple example—

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validation;
use Doctrine\Common\Annotations\AnnotationRegistry;

AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\Validator\Constraints', __DIR__);

class Parent
{
    /**
     * @Assert\EqualTo(0)
     */
    public $example = 0;
}

class Child extends Parent
{
    /**
     * @Assert\EqualTo(1)
     */
    public $example = 1;    // overrides parent property of same name
}

echo Validation::createValidatorBuilder()
               ->enableAnnotationMapping()
               ->getValidator()
               ->validate(new Child());

The validation fails because the constraint that was defined on Parent::$example has been merged with that for Child::$example (and, in this case, they are mutually exclusive).

This cannot be right—the child property has completely replaced that of the parent and all of the latter's metadata (including validation constraints) should be discarded.

For the time being, I have added the following two lines after AnnotationLoader.php:61:

                unset($metadata->members[$property->name]);
                unset($metadata->properties[$property->name]);

However, I acknowledge that this is horrible—at very least, ClassMetadata should provide a method for resetting a property, but even if one could use that I'm not familiar enough with the code to be certain that this approach doesn't break something else. Grateful for your thoughts!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.