10000 Merge branch '3.4' into 4.4 · symfony/symfony@803a257 · GitHub
[go: up one dir, main page]

Skip to content

Commit 803a257

Browse files
committed
Merge branch '3.4' into 4.4
* 3.4: [Serializer] Fix variadic support when using type hints
2 parents 7f2726a + 688dbd9 commit 803a257

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
483483
*/
484484
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null)
485485
{
486-
if (null === $this->propertyTypeExtractor || null === $this->propertyTypeExtractor->getTypes($class->getName(), $parameterName)) {
486+
if ($parameter->isVariadic() || null === $this->propertyTypeExtractor || null === $this->propertyTypeExtractor->getTypes($class->getName(), $parameterName)) {
487487
return parent::denormalizeParameter($class, $parameter, $parameterName, $parameterData, $context, $format);
488488
}
489489

src/Symfony/Component/Serializer/Tests/Fixtures/VariadicConstructorTypedArgsDummy.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function __construct(Dummy ...$foo)
2020
$this->foo = $foo;
2121
}
2222

23+
/** @return Dummy[] */
2324
public function getFoo()
2425
{
2526
return $this->foo;

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use PHPUnit\Framework\MockObject\MockObject;
66
use PHPUnit\Framework\TestCase;
7+
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
8+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
79
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
810
use Symfony\Component\Serializer\Mapping\ClassMetadata;
911
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
@@ -121,17 +123,48 @@ public function testObjectWithNullableConstructorArgument()
121123
$this->assertNull($dummy->getFoo());
122124
}
123125

124-
public function testObjectWithVariadicConstructorTypedArguments()
126+
/**
127+
* @dataProvider getNormalizer
128+
*/
129+
public function testObjectWithVariadicConstructorTypedArguments(AbstractNormalizer $normalizer)
125130
{
126-
$normalizer = new PropertyNormalizer();
127-
$normalizer->setSerializer(new Serializer([$normalizer]));
128-
$data = ['foo' => [['foo' => 'Foo', 'bar' => 'Bar', 'baz' => 'Baz', 'qux' => 'Qux'], ['foo' => 'FOO', 'bar' => 'BAR', 'baz' => 'BAZ', 'qux' => 'QUX']]];
129-
$dummy = $normalizer->denormalize($data, VariadicConstructorTypedArgsDummy::class);
131+
$d1 = new Dummy();
132+
$d1->foo = 'Foo';
133+
$d1->bar = 'Bar';
134+
$d1->baz = 'Baz';
135+
$d1->qux = 'Quz';
136+
$d2 = new Dummy();
137+
$d2->foo = 'FOO';
138+
$d2->bar = 'BAR';
139+
$d2->baz = 'BAZ';
140+
$d2->qux = 'QUZ';
141+
$obj = new VariadicConstructorTypedArgsDummy($d1, $d2);
142+
143+
$serializer = new Serializer([$normalizer], [new JsonEncoder()]);
144+
$normalizer->setSerializer($serializer);
145+
$data = $serializer->serialize($obj, 'json');
146+
$dummy = $normalizer->denormalize(json_decode($data, true), VariadicConstructorTypedArgsDummy::class);
147+
$this->assertInstanceOf(VariadicConstructorTypedArgsDummy::class, $dummy);
148+
$this->assertCount(2, $dummy->getFoo());
149+
foreach ($dummy->getFoo() as $foo) {
150+
$this->assertInstanceOf(Dummy::class, $foo);
151+
}
130152

153+
$dummy = $serializer->deserialize($data, VariadicConstructorTypedArgsDummy::class, 'json');
131154
$this->assertInstanceOf(VariadicConstructorTypedArgsDummy::class, $dummy);
132155
$this->assertCount(2, $dummy->getFoo());
133156
foreach ($dummy->getFoo() as $foo) {
134157
$this->assertInstanceOf(Dummy::class, $foo);
135158
}
136159
}
160+
161+
public function getNormalizer()
162+
{
163+
$extractor = new PhpDocExtractor();
164+
165+
yield [new PropertyNormalizer()];
166+
yield [new PropertyNormalizer(null, null, $extractor)];
167+
yield [new ObjectNormalizer()];
168+
yield [new ObjectNormalizer(null, null, null, $extractor)];
169+
}
137170
}

0 commit comments

Comments
 (0)
0