Skip to content

Commit e28c06a

Browse files
committed
Merge branch '5.0'
* 5.0: Fix wrong namespaces Fix wrong namespaces Fix the reporting of deprecations in twig:lint forward multiple attributes voting flag bumped Symfony version to 5.0.8 updated VERSION for 5.0.7 updated CHANGELOG for 5.0.7 bumped Symfony version to 4.4.8 updated VERSION for 4.4.7 updated CHANGELOG for 4.4.7 [Validator] Fixed calling getters before resolving groups [HttpKernel][LoggerDataCollector] Prevent keys collisions in the sanitized logs processing
2 parents 0fc7cdd + 2b9bd80 commit e28c06a

File tree

5 files changed

+102
-3
lines changed

5 files changed

+102
-3
lines changed

Context/ExecutionContext.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Validator\Mapping\MetadataInterface;
2121
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
2222
use Symfony\Component\Validator\Util\PropertyPath;
23+
use Symfony\Component\Validator\Validator\LazyProperty;
2324
use Symfony\Component\Validator\Validator\ValidatorInterface;
2425
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
2526
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
@@ -180,7 +181,7 @@ public function addViolation(string $message, array $parameters = [])
180181
$parameters,
181182
$this->root,
182183
$this->propertyPath,
183-
$this->value,
184+
$this->getValue(),
184185
null,
185186
null,
186187
$this->constraint
@@ -199,7 +200,7 @@ public function buildViolation(string $message, array $parameters = []): Constra
199200
$parameters,
200201
$this->root,
201202
$this->propertyPath,
202-
$this->value,
203+
$this->getValue(),
203204
$this->translator,
204205
$this->translationDomain
205206
);
@@ -234,6 +235,10 @@ public function getRoot()
234235
*/
235236
public function getValue()
236237
{
238+
if ($this->value instanceof LazyProperty) {
239+
return $this->value->getPropertyValue();
240+
}
241+
237242
return $this->value;
238243
}
239244

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Fixtures;
13+
14+
class EntityWithGroupedConstraintOnMethods
15+
{
16+
public $bar;
17+
18+
public function isValidInFoo()
19+
{
20+
return false;
21+
}
22+
23+
public function getBar()
24+
{
25+
throw new \Exception('Should not be called');
26+
}
27+
}

Tests/Validator/RecursiveValidatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@
1414
use Symfony\Component\Translation\IdentityTranslator;
1515
use Symfony\Component\Validator\Constraints\All;
1616
use Symfony\Component\Validator\Constraints\Collection;
17+
use Symfony\Component\Validator\Constraints\GroupSequence;
18+
use Symfony\Component\Validator\Constraints\IsTrue;
1719
use Symfony\Component\Validator\Constraints\Length;
1820
use Symfony\Component\Validator\Constraints\NotBlank;
21+
use Symfony\Component\Validator\Constraints\NotNull;
1922
use Symfony\Component\Validator\ConstraintValidatorFactory;
2023
use Symfony\Component\Validator\Context\ExecutionContextFactory;
24+
use Symfony\Component\Validator\Mapping\ClassMetadata;
2125
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
2226
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildA;
2327
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildB;
2428
use Symfony\Component\Validator\Tests\Fixtures\Entity;
29+
use Symfony\Component\Validator\Tests\Fixtures\EntityWithGroupedConstraintOnMethods;
2530
use Symfony\Component\Validator\Validator\RecursiveValidator;
2631
use Symfony\Component\Validator\Validator\ValidatorInterface;
2732

@@ -118,6 +123,25 @@ public function testCollectionConstraintValidateAllGroupsForNestedConstraints()
118123
$this->assertInstanceOf(NotBlank::class, $violations->get(1)->getConstraint());
119124
}
120125

126+
public function testGroupedMethodConstraintValidateInSequence()
127+
{
128+
$metadata = new ClassMetadata(EntityWithGroupedConstraintOnMethods::class);
129+
$metadata->addPropertyConstraint('bar', new NotNull(['groups' => 'Foo']));
130+
$metadata->addGetterMethodConstraint('validInFoo', 'isValidInFoo', new IsTrue(['groups' => 'Foo']));
131+
$metadata->addGetterMethodConstraint('bar', 'getBar', new NotNull(['groups' => 'Bar']));
132+
133+
$this->metadataFactory->addMetadata($metadata);
134+
135+
$entity = new EntityWithGroupedConstraintOnMethods();
136+
$groups = new GroupSequence(['EntityWithGroupedConstraintOnMethods', 'Foo', 'Bar']);
137+
138+
$violations = $this->validator->validate($entity, null, $groups);
139+
140+
$this->assertCount(2, $violations);
141+
$this->assertInstanceOf(NotNull::class, $violations->get(0)->getConstraint());
142+
$this->assertInstanceOf(IsTrue::class, $violations->get(1)->getConstraint());
143+
}
144+
121145
public function testAllConstraintValidateAllGroupsForNestedConstraints()
122146
{
123147
$this->metadata->addPropertyConstraint('data', new All(['constraints' => [

Validator/LazyProperty.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Validator;
13+
14+
/**
15+
* A wrapper for a callable initializing a property from a getter.
16+
*
17+
* @internal
18+
*/
19+
class LazyProperty
20+
{
21+
private $propertyValueCallback;
22+
23+
public function __construct(\Closure $propertyValueCallback)
24+
{
25+
$this->propertyValueCallback = $propertyValueCallback;
26+
}
27+
28+
public function getPropertyValue()
29+
{
30+
return \call_user_func($this->propertyValueCallback);
31+
}
32+
}

Validator/RecursiveContextualValidator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
2929
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
3030
use Symfony\Component\Validator\Mapping\GenericMetadata;
31+
use Symfony\Component\Validator\Mapping\GetterMetadata;
3132
use Symfony\Component\Validator\Mapping\MetadataInterface;
3233
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
3334
use Symfony\Component\Validator\Mapping\TraversalStrategy;
@@ -502,7 +503,13 @@ private function validateClassNode(object $object, ?string $cacheKey, ClassMetad
502503
throw new UnsupportedMetadataException(sprintf('The property metadata instances should implement "Symfony\Component\Validator\Mapping\PropertyMetadataInterface", got: "%s".', get_debug_type($propertyMetadata)));
503504
}
504505

505-
$propertyValue = $propertyMetadata->getPropertyValue($object);
506+
if ($propertyMetadata instanceof GetterMetadata) {
507+
$propertyValue = new LazyProperty(static function () use ($propertyMetadata, $object) {
508+
return $propertyMetadata->getPropertyValue($object);
509+
});
510+
} else {
511+
$propertyValue = $propertyMetadata->getPropertyValue($object);
512+
}
506513

507514
$this->validateGenericNode(
508515
$propertyValue,
@@ -729,6 +736,10 @@ private function validateInGroup($value, ?string $cacheKey, MetadataInterface $m
729736
$validator = $this->validatorFactory->getInstance($constraint);
730737
$validator->initialize($context);
731738

739+
if ($value instanceof LazyProperty) {
740+
$value = $value->getPropertyValue();
741+
}
742+
732743
try {
733744
$validator->validate($value, $constraint);
734745
} catch (UnexpectedValueException $e) {

0 commit comments

Comments
 (0)