Commit 04652b7
committed
bug #54137 [Validator] UniqueValidator - normalize before reducing keys (Brajk19)
This PR was squashed before being merged into the 6.4 branch.
Discussion
----------
[Validator] UniqueValidator - normalize before reducing keys
| Q | A
| ------------- | ---
| Branch? | 6.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Issues |
| License | MIT
In #42403 checking for uniqueness of certain collection keys was enabled. Method `UniqueValidator::reduceElementKeys` removes all keys which are not specified.
Problem is that this happens before normalization, which in my opinion is not great because that method accepts array argument and if i have some object (DTO), TypeError will be thrown.
Example:
```php
class ParentDTO
{
/**
* `@var` ChildDTO[]
*/
#[Assert\Unique(
normalizer: [ChildDTO::class, 'normalize']
fields: 'id'
)]
public array $children;
}
```
```php
class ChildDTO
{
public string $id;
public string $name;
public static function normalize(self $obj): array
{
return [
'id' => $obj->id,
'name' => $obj->name
];
}
}
```
Because normalization will happen after `reduceElementKeys` this will be thrown:
`TypeError: Symfony\Component\Validator\Constraints\UniqueValidator::reduceElementKeys(): Argument #2 ($element) must be of type array, ...\ChildDTO given, called in .../UniqueValidator.php on line 48`
If `$element = $normalizer($element);` is executed before `reduceElementKeys` it would enable using Assert\Unique with array of objects when correctly normalized
Commits
-------
77df90b [Validator] UniqueValidator - normalize before reducing keysFile tree
2 files changed
+33
-2
lines changed- src/Symfony/Component/Validator
- Constraints
- Tests/Constraints
2 files changed
+33
-2
lines changedLines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
50 | | - | |
51 | | - | |
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| |||
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
283 | 284 | | |
284 | 285 | | |
285 | 286 | | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
286 | 317 | | |
287 | 318 | | |
288 | 319 | | |
| |||
0 commit comments