Closed
Description
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!