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

Skip to content

[Serializer] Argument objects #19277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 11, 2016
19 changes: 16 additions & 3 deletions src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,16 @@ protected function getConstructor(array &$data, $class, array &$context, \Reflec
* @param array $context
* @param \ReflectionClass $reflectionClass
* @param array|bool $allowedAttributes
* @param string|null $format
*
* @return object
*
* @throws RuntimeException
*/
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes/*, $format = null*/)
{
$format = func_num_args() >= 6 ? func_get_arg(5) : null;

if (
isset($context[static::OBJECT_TO_POPULATE]) &&
is_object($context[static::OBJECT_TO_POPULATE]) &&
Expand Down Expand Up @@ -319,8 +322,18 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
$params = array_merge($params, $data[$paramName]);
}
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removing the unset?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad copy/paste, should be kept

$params[] = $data[$key];
// don't run set for a parameter passed to the constructor
$parameterData = $data[$key];
try {
if (null !== $constructorParameter->getClass()) {
$parameterClass = $constructorParameter->getClass()->getName();
$parameterData = $this->serializer->deserialize($parameterData, $parameterClass, $format, $context);
}
} catch (\ReflectionException $e) {
throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e);
}

// Don't run set for a parameter passed to the constructor
$params[] = $parameterData;
unset($data[$key]);
} elseif ($constructorParameter->isDefaultValueAvailable()) {
$params[] = $constructorParameter->getDefaultValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
$normalizedData = $this->prepareForDenormalization($data);

$reflectionClass = new \ReflectionClass($class);
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);

foreach ($normalizedData as $attribute => $value) {
if ($this->nameConverter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
$normalizedData = $this->prepareForDenormalization($data);

$reflectionClass = new \ReflectionClass($class);
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);

$classMethods = get_class_methods($object);
foreach ($normalizedData as $attribute => $value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Symfony\Component\Serializer\Tests\Fixtures;

use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @author Théo FIDRY <[email protected]>
*/
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ public function testDenormalize()
$this->assertNull($normalizedData->bar);
$this->assertSame('baz', $normalizedData->baz);
}

/**
* @group legacy
*/
public function testInstantiateObjectDenormalizer()
{
$data = array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz');
$class = __NAMESPACE__.'\Dummy';
$context = array();

$normalizer = new AbstractObjectNormalizerDummy();
$normalizer->instantiateObject($data, $class, $context, new \ReflectionClass($class), array());
}
}

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand All @@ -45,6 +58,11 @@ protected function isAllowedAttribute($classOrObject, $attribute, $format = null
{
return in_array($attribute, array('foo', 'baz'));
}

public function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
{
return parent::instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes);
}
}

class Dummy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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 @@ -157,6 +158,49 @@ public function testConstructorWithObjectDenormalize()
$this->assertEquals('bar', $obj->bar);
}

public function testConstructorWithObjectTypeHintDenormalize()
{
$data = array(
'id' => 10,
'inner' => array(
'foo' => 'oof',
'bar' => 'rab',
),
);

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

$obj = $normalizer->denormalize($data, DummyWithConstructorObject::class);
$this->assertInstanceOf(DummyWithConstructorObject::class, $obj);
$this->assertEquals(10, $obj->getId());
$this->assertInstanceOf(ObjectInner::class, $obj->getInner());
$this->assertEquals('oof', $obj->getInner()->foo);
$this->assertEquals('rab', $obj->getInner()->bar);
}

/**
* @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
* @expectedExceptionMessage Could not determine the class of the parameter "unknown".
*/
public function testConstructorWithUnknownObjectTypeHintDenormalize()
{
$data = array(
'id' => 10,
'unknown' => array(
'foo' => 'oof',
'bar' => 'rab',
),
);

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

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

public function testGroupsNormalize()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
Expand Down Expand Up @@ -782,3 +826,32 @@ protected function isAllowedAttribute($classOrObject, $attribute, $format = null
return false;
}
}

class DummyWithConstructorObject
{
private $id;
private $inner;

public function __construct($id, ObjectInner $inner)
{
$this->id = $id;
$this->inner = $inner;
}

public function getId()
{
return $this->id;
}

public function getInner()
{
return $this->inner;
}
}

class DummyWithConstructorInexistingObject
{
public function __construct($id, Unknown $unknown)
{
}
}