diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 82f6e1e2a8f3..cd46b5b97d20 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -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; @@ -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); diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/DenormalizerDecoratorSerializer.php b/src/Symfony/Component/Serializer/Tests/Fixtures/DenormalizerDecoratorSerializer.php deleted file mode 100644 index 06d36c4bd85c..000000000000 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/DenormalizerDecoratorSerializer.php +++ /dev/null @@ -1,43 +0,0 @@ - - */ -class DenormalizerDecoratorSerializer implements SerializerInterface -{ - private $normalizer; - - /** - * @param NormalizerInterface|DenormalizerInterface $normalizer - */ - public function __construct($normalizer) - { - if (false === $normalizer instanceof NormalizerInterface && false === $normalizer instanceof DenormalizerInterface) { - throw new \InvalidArgumentException(); - } - - $this->normalizer = $normalizer; - } - - /** - * {@inheritdoc} - */ - public function serialize($data, $format, array $context = array()) - { - return $this->normalizer->normalize($data, $format, $context); - } - - /** - * {@inheritdoc} - */ - public function deserialize($data, $type, $format, array $context = array()) - { - return $this->normalizer->denormalize($data, $type, $format, $context); - } -} diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 161145bd9749..e1d3a905ecf7 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -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; @@ -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); @@ -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); diff --git a/src/Symfony/Component/Serializer/Tests/SerializerTest.php b/src/Symfony/Component/Serializer/Tests/SerializerTest.php index 362357864c46..ff7820c0d07e 100644 --- a/src/Symfony/Component/Serializer/Tests/SerializerTest.php +++ b/src/Symfony/Component/Serializer/Tests/SerializerTest.php @@ -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 @@ -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; + } +}