Skip to content

Commit 52ddce1

Browse files
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 ------- 776daf2 fix validating lazy properties that evaluate to null
2 parents 034be44 + 776daf2 commit 52ddce1

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/Symfony/Component/Validator/Tests/Fixtures/EntityParent.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class EntityParent implements EntityInterfaceA
1818
protected $firstName;
1919
private $internal;
2020
private $data = 'Data';
21+
private $child;
2122

2223
/**
2324
* @NotNull
@@ -28,4 +29,9 @@ public function getData()
2829
{
2930
return 'Data';
3031
}
32+
33+
public function getChild()
34+
{
35+
return $this->child;
36+
}
3137
}

src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
use Symfony\Component\Validator\Constraints\NotNull;
2222
use Symfony\Component\Validator\Constraints\Optional;
2323
use Symfony\Component\Validator\Constraints\Required;
24+
use Symfony\Component\Validator\Constraints\Valid;
2425
use Symfony\Component\Validator\ConstraintValidatorFactory;
2526
use Symfony\Component\Validator\Context\ExecutionContextFactory;
2627
use Symfony\Component\Validator\Mapping\ClassMetadata;
2728
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
2829
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildA;
2930
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildB;
3031
use Symfony\Component\Validator\Tests\Fixtures\Entity;
32+
use Symfony\Component\Validator\Tests\Fixtures\EntityParent;
3133
use Symfony\Component\Validator\Tests\Fixtures\EntityWithGroupedConstraintOnMethods;
3234
use Symfony\Component\Validator\Validator\RecursiveValidator;
3335

@@ -143,6 +145,31 @@ public function testGroupedMethodConstraintValidateInSequence()
143145
$this->assertInstanceOf(IsTrue::class, $violations->get(1)->getConstraint());
144146
}
145147

148+
public function testValidConstraintOnGetterReturningNull()
149+
{
150+
$metadata = new ClassMetadata(EntityParent::class);
151+
$metadata->addGetterConstraint('child', new Valid());
152+
153+
$this->metadataFactory->addMetadata($metadata);
154+
155+
$violations = $this->validator->validate(new EntityParent());
156+
157+
$this->assertCount(0, $violations);
158+
}
159+
160+
public function testNotNullConstraintOnGetterReturningNull()
161+
{
162+
$metadata = new ClassMetadata(EntityParent::class);
163+
$metadata->addGetterConstraint('child', new NotNull());
164+
165+
$this->metadataFactory->addMetadata($metadata);
166+
167+
$violations = $this->validator->validate(new EntityParent());
168+
169+
$this->assertCount(1, $violations);
170+
$this->assertInstanceOf(NotNull::class, $violations->get(0)->getConstraint());
171+
}
172+
146173
public function testAllConstraintValidateAllGroupsForNestedConstraints()
147174
{
148175
$this->metadata->addPropertyConstraint('data', new All(['constraints' => [

src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,10 @@ private function validateGenericNode($value, $object, $cacheKey, MetadataInterfa
680680

681681
if ($value instanceof LazyProperty) {
682682
$value = $value->getPropertyValue();
683+
684+
if (null === $value) {
685+
return;
686+
}
683687
}
684688

685689
if (\is_array($value)) {

0 commit comments

Comments
 (0)