Skip to content

[Validator] fix validating lazy properties that evaluate to null #37447

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

Merged
merged 1 commit into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class EntityParent implements EntityInterfaceA
protected $firstName;
private $internal;
private $data = 'Data';
private $child;

/**
* @NotNull
Expand All @@ -28,4 +29,9 @@ public function getData()
{
return 'Data';
}

public function getChild()
{
return $this->child;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Context\ExecutionContextFactory;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildA;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildB;
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\Validator\RecursiveValidator;

Expand Down Expand Up @@ -143,6 +145,31 @@ public function testGroupedMethodConstraintValidateInSequence()
$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()
{
$this->metadata->addPropertyConstraint('data', new All(['constraints' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,10 @@ private function validateGenericNode($value, $object, $cacheKey, MetadataInterfa

if ($value instanceof LazyProperty) {
$value = $value->getPropertyValue();

if (null === $value) {
return;
}
}

if (\is_array($value)) {
Expand Down