8000 [Validator] Fixed calling getters before resolving groups by HeahDude · Pull Request #36245 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Fixed calling getters before resolving groups #36245

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
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
9 changes: 7 additions & 2 deletions src/Symfony/Component/Validator/Context/ExecutionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Validator\Mapping\MetadataInterface;
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
use Symfony\Component\Validator\Util\PropertyPath;
use Symfony\Component\Validator\Validator\LazyProperty;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;

Expand Down Expand Up @@ -187,7 +188,7 @@ public function addViolation($message, array $parameters = [])
$parameters,
$this->root,
$this->propertyPath,
$this->value,
$this->getValue(),
null,
null,
$this->constraint
Expand All @@ -206,7 +207,7 @@ public function buildViolation($message, array $parameters = [])
$parameters,
$this->root,
$this->propertyPath,
$this->value,
$this->getValue(),
$this->translator,
$this->translationDomain
);
Expand Down Expand Up @@ -241,6 +242,10 @@ public function getRoot()
*/
public function getValue()
{
if ($this->value instanceof LazyProperty) {
return $this->value->getPropertyValue();
}

return $this->value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Fixtures;

class EntityWithGroupedConstraintOnMethods
{
public $bar;

public function isValidInFoo()
{
return false;
}

public function getBar()
{
throw new \Exception('Should not be called');
}
}
Original file line nu 8000 mber Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
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\EntityWithGroupedConstraintOnMethods;
use Symfony\Component\Validator\Validator\RecursiveValidator;

class RecursiveValidatorTest extends AbstractTest
Expand Down Expand Up @@ -117,6 +122,25 @@ public function testCollectionConstraintValidateAllGroupsForNestedConstraints()
$this->assertInstanceOf(NotBlank::class, $violations->get(1)->getConstraint());
}

public function testGroupedMethodConstraintValidateInSequence()
{
$metadata = new ClassMetadata(EntityWithGroupedConstraintOnMethods::class);
$metadata->addPropertyConstraint('bar', new NotNull(['groups' => 'Foo']));
$metadata->addGetterMethodConstraint('validInFoo', 'isValidInFoo', new IsTrue(['groups' => 'Foo']));
$metadata->addGetterMethodConstraint('bar', 'getBar', new NotNull(['groups' => 'Bar']));

$this->metadataFactory->addMetadata($metadata);

$entity = new EntityWithGroupedConstraintOnMethods();
$groups = new GroupSequence(['EntityWithGroupedConstraintOnMethods', 'Foo', 'Bar']);

$violations = $this->validator->validate($entity, null, $groups);

$this->assertCount(2, $violations);
$this->assertInstanceOf(NotNull::class, $violations->get(0)->getConstraint());
$this->assertInstanceOf(IsTrue::class, $violations->get(1)->getConstraint());
}

public function testAllConstraintValidateAllGroupsForNestedConstraints()
{
$this->metadata->addPropertyConstraint('data', new All(['constraints' => [
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Validator/Validator/LazyProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Validator;

/**
* A wrapper for a callable initializing a property from a getter.
*
* @internal
*/
class LazyProperty
{
private $propertyValueCallback;

public function __construct(\Closure $propertyValueCallback)
{
$this->propertyValueCallback = $propertyValueCallback;
}

public function getPropertyValue()
{
return \call_user_func($this->propertyValueCallback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\GenericMetadata;
use Symfony\Component\Validator\Mapping\GetterMetadata;
use Symfony\Component\Validator\Mapping\MetadataInterface;
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
use Symfony\Component\Validator\Mapping\TraversalStrategy;
Expand Down Expand Up @@ -534,7 +535,13 @@ private function validateClassNode($object, $cacheKey, ClassMetadataInterface $m
throw new UnsupportedMetadataException(sprintf('The property metadata instances should implement "Symfony\Component\Validator\Mapping\PropertyMetadataInterface", got: "%s".', \is_object($propertyMetadata) ? \get_class($propertyMetadata) : \gettype($propertyMetadata)));
}

$propertyValue = $propertyMetadata->getPropertyValue($object);
if ($propertyMetadata instanceof GetterMetadata) {
$propertyValue = new LazyProperty(static function () use ($propertyMetadata, $object) {
return $propertyMetadata->getPropertyValue($object);
});
} else {
$propertyValue = $propertyMetadata->getPropertyValue($object);
}

$this->validateGenericNode(
$propertyValue,
Expand Down Expand Up @@ -798,6 +805,11 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,

$validator = $this->validatorFactory->getInstance($constraint);
$validator->initialize($context);

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

$validator->validate($value, $constraint);
}
}
Expand Down
0