8000 treat uninitialized properties referenced by property paths as null · symfony/symfony@1b35b84 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b35b84

Browse files
committed
treat uninitialized properties referenced by property paths as null
1 parent e507619 commit 1b35b84

12 files changed

+152
-1
lines changed

src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
15+
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1516
use Symfony\Component\PropertyAccess\PropertyAccess;
1617
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1718
use Symfony\Component\Validator\Constraint;
@@ -56,6 +57,8 @@ public function validate($value, Constraint $constraint)
5657
$comparedValue = $this->getPropertyAccessor()->getValue($object, $path);
5758
} catch (NoSuchPropertyException $e) {
5859
throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: ', $path, get_debug_type($constraint)).$e->getMessage(), 0, $e);
60+
} catch (UninitializedPropertyException $e) {
61+
$comparedValue = null;
5962
}
6063
} else {
6164
$comparedValue = $constraint->value;

src/Symfony/Component/Validator/Constraints/BicValidator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Intl\Countries;
1515
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
16+
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1617
use Symfony\Component\PropertyAccess\PropertyAccess;
1718
use Symfony\Component\PropertyAccess\PropertyAccessor;
1819
use Symfony\Component\Validator\Constraint;
@@ -130,6 +131,8 @@ public function validate($value, Constraint $constraint)
130131
$iban = $this->getPropertyAccessor()->getValue($object, $path);
131132
} catch (NoSuchPropertyException $e) {
132133
throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: ', $path, get_debug_type($constraint)).$e->getMessage(), 0, $e);
134+
} catch (UninitializedPropertyException $e) {
135+
$iban = null;
133136
}
134137
}
135138
if (!$iban) {

src/Symfony/Component/Validator/Constraints/RangeValidator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
15+
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1516
use Symfony\Component\PropertyAccess\PropertyAccess;
1617
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1718
use Symfony\Component\Validator\Constraint;
@@ -178,6 +179,8 @@ private function getLimit(?string $propertyPath, $default, Constraint $constrain
178179
return $this->getPropertyAccessor()->getValue($object, $propertyPath);
179180
} catch (NoSuchPropertyException $e) {
180181
throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: ', $propertyPath, get_debug_type($constraint)).$e->getMessage(), 0, $e);
182+
} catch (UninitializedPropertyException $e) {
183+
return null;
181184
}
182185
}
183186

src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Constraints\AbstractComparison;
1717
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1818
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
19+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\TypedDummy;
1920

2021
class ComparisonTest_Class
2122
{
@@ -274,6 +275,33 @@ public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsS
274275
}
275276
}
276277

278+
/**
279+
* @requires PHP 7.4
280+
*
281+
* @dataProvider provideComparisonsToNullValueAtPropertyPath
282+
*/
283+
public function testCompareWithUninitializedPropertyAtPropertyPath($dirtyValue, $dirtyValueAsString, $isValid)
284+
{
285+
$this->setObject(new TypedDummy());
286+
287+
$this->validator->validate($dirtyValue, $this->createConstraint([
288+
'message' => 'Constraint Message',
289+
'propertyPath' => 'value',
290+
]));
291+
292+
if ($isValid) {
293+
$this->assertNoViolation();
294+
} else {
295+
$this->buildViolation('Constraint Message')
296+
->setParameter('{{ value }}', $dirtyValueAsString)
297+
->setParameter('{{ compared_value }}', 'null')
298+
->setParameter('{{ compared_value_type }}', 'null')
299+
->setParameter('{{ compared_value_path }}', 'value')
300+
->setCode($this->getErrorCode())
301+
->assertRaised();
302+
}
303+
}
304+
277305
public static function provideAllInvalidComparisons(): array
278306
{
279307
// The provider runs before setUp(), so we need to manually fix

src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Validator\Mapping\ClassMetadata;
1919
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
2020
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
21+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\BicTypedDummy;
2122

2223
class BicValidatorTest extends ConstraintValidatorTestCase
2324
{
@@ -92,6 +93,18 @@ public function testInvalidComparisonToPropertyPathFromAttribute()
9293
->assertRaised();
9394
}
9495

96+
/**
97+
* @requires PHP 7.4
98+
*/
99+
public function testPropertyPathReferencingUninitializedProperty()
100+
{
101+
$this->setObject(new BicTypedDummy());
102+
103+
$this->validator->validate('UNCRIT2B912', new Bic(['ibanPropertyPath' => 'iban']));
104+
105+
$this->assertNoViolation();
106+
}
107+
95108
public function testValidComparisonToValue()
96109
{
97110
$constraint = new Bic(['iban' => 'FR14 2004 1010 0505 0001 3M02 606']);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Constraints\Fixtures;
13+
14+
class BicTypedDummy
15+
{
16+
public string $iban;
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Constraints\Fixtures;
13+
14+
class MinMaxTyped
15+
{
16+
public int $min;
17+
public int $max;
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Constraints\Fixtures;
13+
14+
class TypedDummy
15+
{
16+
public mixed $value;
17+
}

src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorWithNegativeOrZeroConstraintTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsS
121121
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
122122
}
123123

124+
/**
125+
* @requires PHP 7.4
126+
*
127+
* @dataProvider provideComparisonsToNullValueAtPropertyPath
128+
*/
129+
public function testCompareWithUninitializedPropertyAtPropertyPath($dirtyValue, $dirtyValueAsString, $isValid)
130+
{
131+
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
132+
}
133+
124134
public static function throwsOnInvalidStringDatesProvider(): array
125135
{
126136
self::markTestSkipped('The "value" option cannot be used in the NegativeOrZero constraint');

src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorWithNegativeConstraintTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsS
121121
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
122122
}
123123

124+
/**
125+
* @requires PHP 7.4
126+
*
127+
* @dataProvider provideComparisonsToNullValueAtPropertyPath
128+
*/
129+
public function testCompareWithUninitializedPropertyAtPropertyPath($dirtyValue, $dirtyValueAsString, $isValid)
130+
{
131+
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
132+
}
133+
124134
public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
125135
{
126136
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');

src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Constraints\RangeValidator;
1717
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1818
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
19+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\MinMaxTyped;
1920
use Symfony\Component\Validator\Tests\IcuCompatibilityTrait;
2021

2122
class RangeValidatorTest extends ConstraintValidatorTestCase
@@ -1042,6 +1043,34 @@ public function testInvalidDatesCombinedMinPropertyPath($value, $dateTimeAsStrin
10421043
->assertRaised();
10431044
}
10441045

1046+
/**
1047+
* @requires PHP 7.4
1048+
*/
1049+
public function testMinPropertyPathReferencingUninitializedProperty()
1050+
{
1051+
$object = new MinMaxTyped();
1052+
$object->max = 5;
1053+
$this->setObject($object);
1054+
1055+
$this->validator->validate(5, new Range(['minPropertyPath' => 'min', 'maxPropertyPath' => 'max']));
1056+
1057+
$this->assertNoViolation();
1058+
}
1059+
1060+
/**
1061+
* @requires PHP 7.4
1062+
*/
1063+
public function testMaxPropertyPathReferencingUninitializedProperty()
1064+
{
1065+
$object = new MinMaxTyped();
1066+
$object->min = 5;
1067+
$this->setObject($object);
1068+
1069+
$this->validator->validate(5, new Range(['minPropertyPath' => 'min', 'maxPropertyPath' => 'max']));
1070+
1071+
$this->assertNoViolation();
1072+
}
1073+
10451074
public static function provideMessageIfMinAndMaxSet(): array
10461075
{
10471076
$notInRangeMessage = (new Range(['min' => '']))->notInRangeMessage;

src/Symfony/Component/Validator/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"symfony/expression-language": "^5.1|^6.0",
3939
"symfony/cache": "^4.4|^5.0|^6.0",
4040
"symfony/mime": "^4.4|^5.0|^6.0",
41-
"symfony/property-access": "^4.4|^5.0|^6.0",
41+
"symfony/property-access": "^5.4|^6.0",
4242
"symfony/property-info": "^5.3|^6.0",
4343
"symfony/translation": "^5.4.35|~6.3.12|^6.4.3",
4444
"doctrine/annotations": "^1.13|^2",

0 commit comments

Comments
 (0)
0