8000 [Serializer] Fix argument object denormalization · symfony/symfony@27de65a · GitHub
[go: up one dir, main page]

Skip to content

Commit 27de65a

Browse files
committed
[Serializer] Fix argument object denormalization
1 parent 0e12427 commit 27de65a

File tree

4 files changed

+36
-47
lines changed

4 files changed

+36
-47
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Serializer\Exception\CircularReferenceException;
1515
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16+
use Symfony\Component\Serializer\Exception\LogicException;
1617
use Symfony\Component\Serializer\Exception\RuntimeException;
1718
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
1819
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
@@ -336,8 +337,11 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
336337
$parameterData = $data[$key];
337338
try {
338339
if (null !== $constructorParameter->getClass()) {
340+
if (!$this->serializer instanceof DenormalizerInterface) {
341+
throw new LogicException(sprintf('Cannot create an instance of %s from serialized data because the serializer inject in "%s" is not a denormalizer', $constructorParameter->getClass(), static::class));
342+
}
339343
$parameterClass = $constructorParameter->getClass()->getName();
340-
$parameterData = $this->serializer->deserialize($parameterData, $parameterClass, $format, $context);
344+
$parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $context);
341345
}
342346
} catch (\ReflectionException $e) {
343347
throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e);

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Symfony\Component\Serializer\SerializerInterface;
2424
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2525
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
26-
use Symfony\Component\Serializer\Tests\Fixtures\DenormalizerDecoratorSerializer;
2726
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
2827
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
2928
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
@@ -171,7 +170,7 @@ public function testConstructorWithObjectTypeHintDenormalize()
171170
);
172171

173172
$normalizer = new ObjectNormalizer();
174-
$serializer = new DenormalizerDecoratorSerializer($normalizer);
173+
$serializer = new Serializer(array($normalizer));
175174
$normalizer->setSerializer($serializer);
176175

177176
$obj = $normalizer->denormalize($data, DummyWithConstructorObject::class);
@@ -197,7 +196,7 @@ public function testConstructorWithUnknownObjectTypeHintDenormalize()
197196
);
198197

199198
$normalizer = new ObjectNormalizer();
200-
$serializer = new DenormalizerDecoratorSerializer($normalizer);
199+
$serializer = new Serializer(array($normalizer));
201200
$normalizer->setSerializer($serializer);
202201

203202
$normalizer->denormalize($data, DummyWithConstructorInexistingObject::class);

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,15 @@ public function testDenormalizerAware()
336336

337337
new Serializer(array($denormalizerAware));
338338
}
339+
340+
public function testDeserializeObjectConstructorWithObjectTypeHint()
341+
{
342+
$jsonData = '{"bar":{"value":"baz"}}';
343+
344+
$serializer = new Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder()));
345+
346+
$this->assertEquals(new Foo(new Bar('baz')), $serializer->deserialize($jsonData, Foo::class, 'json'));
347+
}
339348
}
340349

341350
class Model
@@ -381,3 +390,23 @@ public function toArray()
381390
return array('title' => $this->title, 'numbers' => $this->numbers);
382391
}
383392
}
393+
394+
class Foo
395+
{
396+
private $bar;
397+
398+
public function __construct(Bar $bar)
399+
{
400+
$this->bar = $bar;
401+
}
402+
}
403+
404+
class Bar
405+
{
406+
private $value;
407+
408+
public function __construct($value)
409+
{
410+
$this->value = $value;
411+
}
412+
}

0 commit comments

Comments
 (0)
0