Skip to content

Commit

Permalink
[Serializer] Fix bugs reported in b5990be#commitcomment-12301266
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Jul 22, 2015
1 parent 5d841a2 commit 65e9f26
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ protected function prepareForDenormalization($data)
*
* @throws RuntimeException
*/
protected function instantiateObject(array $data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
{
if (
isset($context['object_to_populate']) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
$reflectionClass = new \ReflectionClass($class);
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);

$classMethods = get_class_methods($object);
foreach ($normalizedData as $attribute => $value) {
if ($this->nameConverter) {
$attribute = $this->nameConverter->denormalize($attribute);
Expand All @@ -113,7 +114,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
if ($allowed && !$ignored) {
$setter = 'set'.ucfirst($attribute);

if (method_exists($object, $setter)) {
if (in_array($setter, $classMethods)) {
$object->$setter($value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ public function testConstructorWithObjectDenormalize()
$this->assertEquals('bar', $obj->getBar());
}

public function testConstructorWArgWithPrivateMutator()
{
$obj = $this->normalizer->denormalize(array('foo' => 'bar'), __NAMESPACE__.'\ObjectConstructorArgsWithPrivateMutatorDummy', 'any');
$this->assertEquals('bar', $obj->getFoo());
}

public function testGroupsNormalize()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
Expand Down Expand Up @@ -511,15 +517,21 @@ public function testObjectToPopulate()
public function testDenormalizeNonExistingAttribute()
{
$this->assertEquals(
new PropertyDummy(),
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\PropertyDummy')
new GetSetDummy(),
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\GetSetDummy')
);
}

public function testNoTraversableSupport()
{
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
}

public function testPrivateSetter()
{
$obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy');
$this->assertEquals('bar', $obj->getFoo());
}
}

class GetSetDummy
Expand Down Expand Up @@ -726,3 +738,37 @@ public function getBar_foo()
return $this->bar_foo;
}
}

class ObjectConstructorArgsWithPrivateMutatorDummy
{
private $foo;

public function __construct($foo)
{
$this->setFoo($foo);
}

public function getFoo()
{
return $this->foo;
}

private function setFoo($foo)
{
$this->foo = $foo;
}
}

class ObjectWithPrivateSetterDummy
{
private $foo = 'bar';

public function getFoo()
{
return $this->foo;
}

private function setFoo($foo)
{
}
}

0 comments on commit 65e9f26

Please sign in to comment.