Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 688dbd9

Browse files
committed
bug #37845 [Serializer] Fix variadic support when using type hints (fabpot)
This PR was merged into the 3.4 branch. Discussion ---------- [Serializer] Fix variadic support when using type hints | Q | A | ------------- | --- | Branch? | 3.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | n/a <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | n/a Commits ------- 3fffa96 [Serializer] Fix variadic support when using type hints
2 parents 30d8496 + 3fffa96 commit 688dbd9

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ private function validateAndDenormalize($currentClass, $attribute, $data, $forma
304304
*/
305305
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null)
306306
{
307-
if (null === $this->propertyTypeExtractor || null === $this->propertyTypeExtractor->getTypes($class->getName(), $parameterName)) {
307+
if ((method_exists($parameter, 'isVariadic') && $parameter->isVariadic()) || null === $this->propertyTypeExtractor || null === $this->propertyTypeExtractor->getTypes($class->getName(), $parameterName)) {
308308
return parent::denormalizeParameter($class, $parameter, $parameterName, $parameterData, $context, $format);
309309
}
310310

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: 37 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;
@@ -135,19 +137,49 @@ public function testObjectWithNullableConstructorArgument()
135137
}
136138

137139
/**
140+
* @dataProvider getNormalizer
141+
*
138142
* @requires PHP 5.6
139143
*/
140-
public function testObjectWithVariadicConstructorTypedArguments()
144+
public function testObjectWithVariadicConstructorTypedArguments(AbstractNormalizer $normalizer)
141145
{
142-
$normalizer = new PropertyNormalizer();
143-
$normalizer->setSerializer(new Serializer([$normalizer]));
144-
$data = ['foo' => [['foo' => 'Foo', 'bar' => 'Bar', 'baz' => 'Baz', 'qux' => 'Qux'], ['foo' => 'FOO', 'bar' => 'BAR', 'baz' => 'BAZ', 'qux' => 'QUX']]];
145-
$dummy = $normalizer->denormalize($data, VariadicConstructorTypedArgsDummy::class);
146+
$d1 = new Dummy();
147+
$d1->foo = 'Foo';
148+
$d1->bar = 'Bar';
149+
$d1->baz = 'Baz';
150+
$d1->qux = 'Quz';
151+
$d2 = new Dummy();
152+
$d2->foo = 'FOO';
153+
$d2->bar = 'BAR';
154+
$d2->baz = 'BAZ';
155+
$d2->qux = 'QUZ';
156+
$obj = new VariadicConstructorTypedArgsDummy($d1, $d2);
157+
158+
$serializer = new Serializer([$normalizer], [new JsonEncoder()]);
159+
$normalizer->setSerializer($serializer);
160+
$data = $serializer->serialize($obj, 'json');
161+
$dummy = $normalizer->denormalize(json_decode($data, true), VariadicConstructorTypedArgsDummy::class);
162+
$this->assertInstanceOf(VariadicConstructorTypedArgsDummy::class, $dummy);
163+
$this->assertCount(2, $dummy->getFoo());
164+
foreach ($dummy->getFoo() as $foo) {
165+
$this->assertInstanceOf(Dummy::class, $foo);
166+
}
146167

168+
$dummy = $serializer->deserialize($data, VariadicConstructorTypedArgsDummy::class, 'json');
147169
$this->assertInstanceOf(VariadicConstructorTypedArgsDummy::class, $dummy);
148170
$this->assertCount(2, $dummy->getFoo());
149171
foreach ($dummy->getFoo() as $foo) {
150172
$this->assertInstanceOf(Dummy::class, $foo);
151173
}
152174
}
175+
176+
public function getNormalizer()
177+
{
178+
$extractor = new PhpDocExtractor();
179+
180+
yield [new PropertyNormalizer()];
181+
yield [new PropertyNormalizer(null, null, $extractor)];
182+
yield [new ObjectNormalizer()];
183+
yield [new ObjectNormalizer(null, null, null, $extractor)];
184+
}
153185
}

0 commit comments

Comments
 (0)