Commit 3c2e8b9
committed
bug #64477 [Serializer] Keep collection value type for iterable constructor parameters (ousamabenyounes)
This PR was merged into the 7.4 branch.
Discussion
----------
[Serializer] Keep collection value type for iterable constructor parameters
| Q | A
| ------------- | ---
| Branch? | 7.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Issues | Fix #64444
| License | MIT
Regression from the constructor parameter type-override added in 7.4.6: when a
constructor argument is typed `iterable` (or `array`) but the property type
extractor reports a more precise collection type such as `array<A>`, the
override replaced the rich extractor type with the bare reflection type. The
collection value type (`A`) was lost, so nested items were left un-denormalized
(raw arrays instead of `A` instances).
```php
final class A { public function __construct(public string $foo) {} }
final class B {
/** `@var` array<A> */ public array $items;
/** `@param` iterable<A> $items */
public function __construct(iterable $items) { $this->items = iterator_to_array($items); }
}
// before: B::$items contains stdClass/arrays - after: contains A instances
```
The fix keeps the extractor's collection type when it is already an
`array`/`iterable` compatible with the parameter type, so the value type is
preserved and items are denormalized. Genuinely incompatible types (e.g.
extractor `string` vs parameter `int`) are still overridden as before.
Commits
-------
a359992 [Serializer] Keep collection value type for iterable constructor parameters2 files changed
+39Lines changed: 39 additions & 0 deletions
File tree
Expand file treeCollapse file tree
Open diff view settings
Filter options
- src/Symfony/Component/Serializer
- Normalizer
- Tests/Normalizer
Expand file treeCollapse file tree
Open diff view settings
Collapse file
src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php+1Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
1025 | 1025 | |
1026 | 1026 | |
1027 | 1027 | |
| 1028 | + |
1028 | 1029 | |
1029 | 1030 | |
1030 | 1031 | |
|
Collapse file
src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php+38Lines changed: 38 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
1031 | 1031 | |
1032 | 1032 | |
1033 | 1033 | |
| 1034 | + |
| 1035 | + |
| 1036 | + |
| 1037 | + |
| 1038 | + |
| 1039 | + |
| 1040 | + |
| 1041 | + |
| 1042 | + |
| 1043 | + |
| 1044 | + |
| 1045 | + |
| 1046 | + |
| 1047 | + |
| 1048 | + |
| 1049 | + |
| 1050 | + |
1034 | 1051 | |
1035 | 1052 | |
1036 | 1053 | |
| ||
2208 | 2225 | |
2209 | 2226 | |
2210 | 2227 | |
| 2228 | + |
| 2229 | + |
| 2230 | + |
| 2231 | + |
| 2232 | + |
| 2233 | + |
| 2234 | + |
| 2235 | + |
| 2236 | + |
| 2237 | + |
| 2238 | + |
| 2239 | + |
| 2240 | + |
| 2241 | + |
| 2242 | + |
| 2243 | + |
| 2244 | + |
| 2245 | + |
| 2246 | + |
| 2247 | + |
| 2248 | + |
0 commit comments