Skip to content

Commit 020b35a

Browse files
committed
treat uninitialized properties referenced by property paths as null
1 parent 4a176ce commit 020b35a

File tree

7 files changed

+84
-1
lines changed

7 files changed

+84
-1
lines changed

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/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+
}

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy