Skip to content

Commit

Permalink
bug #20690 [Serializer] Fix argument object denormalization (ogizanagi)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.2 branch.

Discussion
----------

[Serializer] Fix argument object denormalization

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20670
| License       | MIT
| Doc PR        | N/A

Fixes #20670. I've seen #19277 (diff) so I think it's the right thing to do, but I didn't follow the thread at the time, so I may have missed something.

Ping @theofidry, @dunglas.

Commits
-------

27de65a [Serializer] Fix argument object denormalization
  • Loading branch information
dunglas committed Dec 6, 2016
2 parents 63087e5 + 27de65a commit 16cea37
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 47 deletions.
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
Expand Down Expand Up @@ -336,8 +337,11 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
$parameterData = $data[$key];
try {
if (null !== $constructorParameter->getClass()) {
if (!$this->serializer instanceof DenormalizerInterface) {
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));
}
$parameterClass = $constructorParameter->getClass()->getName();
$parameterData = $this->serializer->deserialize($parameterData, $parameterClass, $format, $context);
$parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $context);
}
} catch (\ReflectionException $e) {
throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e);
Expand Down

This file was deleted.

Expand Up @@ -23,7 +23,6 @@
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\DenormalizerDecoratorSerializer;
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
Expand Down Expand Up @@ -171,7 +170,7 @@ public function testConstructorWithObjectTypeHintDenormalize()
);

$normalizer = new ObjectNormalizer();
$serializer = new DenormalizerDecoratorSerializer($normalizer);
$serializer = new Serializer(array($normalizer));
$normalizer->setSerializer($serializer);

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

$normalizer = new ObjectNormalizer();
$serializer = new DenormalizerDecoratorSerializer($normalizer);
$serializer = new Serializer(array($normalizer));
$normalizer->setSerializer($serializer);

$normalizer->denormalize($data, DummyWithConstructorInexistingObject::class);
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Expand Up @@ -336,6 +336,15 @@ public function testDenormalizerAware()

new Serializer(array($denormalizerAware));
}

public function testDeserializeObjectConstructorWithObjectTypeHint()
{
$jsonData = '{"bar":{"value":"baz"}}';

$serializer = new Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder()));

$this->assertEquals(new Foo(new Bar('baz')), $serializer->deserialize($jsonData, Foo::class, 'json'));
}
}

class Model
Expand Down Expand Up @@ -381,3 +390,23 @@ public function toArray()
return array('title' => $this->title, 'numbers' => $this->numbers);
}
}

class Foo
{
private $bar;

public function __construct(Bar $bar)
{
$this->bar = $bar;
}
}

class Bar
{
private $value;

public function __construct($value)
{
$this->value = $value;
}
}

0 comments on commit 16cea37

Please sign in to comment.