Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3c2e8b9

Browse filesBrowse files
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 parameters
2 parents 3bd625f + a359992 commit 3c2e8b9
Copy full SHA for 3c2e8b9

2 files changed

+39Lines changed: 39 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
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 numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara
10251025
if (null !== $parameterType && $parameterTypeResolver ??= class_exists(ReflectionTypeResolver::class) ? new ReflectionTypeResolver() : false) {
10261026
$resolvedParameterType = $parameterTypeResolver->resolve($parameterType, ($parameterTypeContextFactory ??= new TypeContextFactory())->createFromClassName($class->name, $parameter->getDeclaringClass()?->name));
10271027
if ($resolvedParameterType->isSatisfiedBy(static fn (Type $t) => match (true) {
1028+
$t instanceof BuiltinType && \in_array($t->getTypeIdentifier(), [TypeIdentifier::ARRAY, TypeIdentifier::ITERABLE], true) => !$type->isIdentifiedBy(TypeIdentifier::ARRAY) && !$type->isIdentifiedBy(TypeIdentifier::ITERABLE),
10281029
$t instanceof BuiltinType && !\in_array($t->getTypeIdentifier(), [TypeIdentifier::NULL, TypeIdentifier::MIXED], true) => !$type->isIdentifiedBy($t->getTypeIdentifier()),
10291030
$t instanceof ObjectType => !$type->isIdentifiedBy($t->getClassName()),
10301031
default => false,
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 numberDiff line numberDiff line change
@@ -1031,6 +1031,23 @@ public function getSupportedTypes(?string $format): array
10311031
$this->assertSame(42, $result->getEntity()->id);
10321032
}
10331033

1034+
public function testDenormalizeIterableConstructorParameterDenormalizesItems()
1035+
{
1036+
$serializer = new Serializer([
1037+
new ArrayDenormalizer(),
1038+
new ObjectNormalizer(null, null, null, new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()])),
1039+
]);
1040+
1041+
$result = $serializer->denormalize(
1042+
['items' => [['value' => 'foo'], ['value' => 'bar']]],
1043+
DummyWithIterableOfDtos::class,
1044+
);
1045+
1046+
$this->assertInstanceOf(DummyWithIterableOfDtos::class, $result);
1047+
$this->assertContainsOnlyInstancesOf(DummyDtoItem::class, $result->items);
1048+
$this->assertSame(['foo', 'bar'], array_map(static fn (DummyDtoItem $i) => $i->value, $result->items));
1049+
}
1050+
10341051
#[Group('legacy')]
10351052
#[IgnoreDeprecations]
10361053
public function testDenormalizeMixedConstructorParameterUsesExtractorTypeLegacy()
@@ -2208,3 +2225,24 @@ public function getEntity(): ?DummyEntity
22082225
return $this->entity;
22092226
}
22102227
}
2228+
2229+
class DummyDtoItem
2230+
{
2231+
public string $value;
2232+
}
2233+
2234+
class DummyWithIterableOfDtos
2235+
{
2236+
/** @var array<DummyDtoItem> */
2237+
public array $items;
2238+
2239+
/**
2240+
* @param iterable<DummyDtoItem> $items
2241+
*/
2242+
public function __construct(iterable $items)
2243+
{
2244+
$this->items = iterator_to_array((static function () use ($items) {
2245+
yield from $items;
2246+
})());
2247+
}
2248+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.