bug #37447 [Validator] fix validating lazy properties that evaluate to null (xabbuh)
This PR was merged into the 3.4 branch.
Discussion
----------
[Validator] fix validating lazy properties that evaluate to null
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | Fix #37430
| License | MIT
| Doc PR |
Commits
-------
776daf28b4
fix validating lazy properties that evaluate to null
This commit is contained in:
commit
52ddce1a74
@ -18,6 +18,7 @@ class EntityParent implements EntityInterfaceA
|
|||||||
protected $firstName;
|
protected $firstName;
|
||||||
private $internal;
|
private $internal;
|
||||||
private $data = 'Data';
|
private $data = 'Data';
|
||||||
|
private $child;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @NotNull
|
* @NotNull
|
||||||
@ -28,4 +29,9 @@ class EntityParent implements EntityInterfaceA
|
|||||||
{
|
{
|
||||||
return 'Data';
|
return 'Data';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getChild()
|
||||||
|
{
|
||||||
|
return $this->child;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ use Symfony\Component\Validator\Constraints\NotBlank;
|
|||||||
use Symfony\Component\Validator\Constraints\NotNull;
|
use Symfony\Component\Validator\Constraints\NotNull;
|
||||||
use Symfony\Component\Validator\Constraints\Optional;
|
use Symfony\Component\Validator\Constraints\Optional;
|
||||||
use Symfony\Component\Validator\Constraints\Required;
|
use Symfony\Component\Validator\Constraints\Required;
|
||||||
|
use Symfony\Component\Validator\Constraints\Valid;
|
||||||
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
||||||
use Symfony\Component\Validator\Context\ExecutionContextFactory;
|
use Symfony\Component\Validator\Context\ExecutionContextFactory;
|
||||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||||
@ -28,6 +29,7 @@ use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
|
|||||||
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildA;
|
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildA;
|
||||||
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildB;
|
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildB;
|
||||||
use Symfony\Component\Validator\Tests\Fixtures\Entity;
|
use Symfony\Component\Validator\Tests\Fixtures\Entity;
|
||||||
|
use Symfony\Component\Validator\Tests\Fixtures\EntityParent;
|
||||||
use Symfony\Component\Validator\Tests\Fixtures\EntityWithGroupedConstraintOnMethods;
|
use Symfony\Component\Validator\Tests\Fixtures\EntityWithGroupedConstraintOnMethods;
|
||||||
use Symfony\Component\Validator\Validator\RecursiveValidator;
|
use Symfony\Component\Validator\Validator\RecursiveValidator;
|
||||||
|
|
||||||
@ -143,6 +145,31 @@ class RecursiveValidatorTest extends AbstractTest
|
|||||||
$this->assertInstanceOf(IsTrue::class, $violations->get(1)->getConstraint());
|
$this->assertInstanceOf(IsTrue::class, $violations->get(1)->getConstraint());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testValidConstraintOnGetterReturningNull()
|
||||||
|
{
|
||||||
|
$metadata = new ClassMetadata(EntityParent::class);
|
||||||
|
$metadata->addGetterConstraint('child', new Valid());
|
||||||
|
|
||||||
|
$this->metadataFactory->addMetadata($metadata);
|
||||||
|
|
||||||
|
$violations = $this->validator->validate(new EntityParent());
|
||||||
|
|
||||||
|
$this->assertCount(0, $violations);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNotNullConstraintOnGetterReturningNull()
|
||||||
|
{
|
||||||
|
$metadata = new ClassMetadata(EntityParent::class);
|
||||||
|
$metadata->addGetterConstraint('child', new NotNull());
|
||||||
|
|
||||||
|
$this->metadataFactory->addMetadata($metadata);
|
||||||
|
|
||||||
|
$violations = $this->validator->validate(new EntityParent());
|
||||||
|
|
||||||
|
$this->assertCount(1, $violations);
|
||||||
|
$this->assertInstanceOf(NotNull::class, $violations->get(0)->getConstraint());
|
||||||
|
}
|
||||||
|
|
||||||
public function testAllConstraintValidateAllGroupsForNestedConstraints()
|
public function testAllConstraintValidateAllGroupsForNestedConstraints()
|
||||||
{
|
{
|
||||||
$this->metadata->addPropertyConstraint('data', new All(['constraints' => [
|
$this->metadata->addPropertyConstraint('data', new All(['constraints' => [
|
||||||
|
@ -680,6 +680,10 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
|
|||||||
|
|
||||||
if ($value instanceof LazyProperty) {
|
if ($value instanceof LazyProperty) {
|
||||||
$value = $value->getPropertyValue();
|
$value = $value->getPropertyValue();
|
||||||
|
|
||||||
|
if (null === $value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (\is_array($value)) {
|
if (\is_array($value)) {
|
||||||
|
Reference in New Issue
Block a user