Skip to content

Commit 143d318

Browse files
committed
[Serializer] Fix bugs reported in symfony/symfony@b5990be#commitcomment-12301266
1 parent 15308f0 commit 143d318

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

Normalizer/AbstractNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ protected function prepareForDenormalization($data)
291291
*
292292
* @throws RuntimeException
293293
*/
294-
protected function instantiateObject(array $data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
294+
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
295295
{
296296
if (
297297
isset($context['object_to_populate']) &&

Normalizer/GetSetMethodNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
102102
$reflectionClass = new \ReflectionClass($class);
103103
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
104104

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

116-
if (method_exists($object, $setter)) {
117+
if (in_array($setter, $classMethods)) {
117118
$object->$setter($value);
118119
}
119120
}

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ public function testConstructorWithObjectDenormalize()
228228
$this->assertEquals('bar', $obj->getBar());
229229
}
230230

231+
public function testConstructorWArgWithPrivateMutator()
232+
{
233+
$obj = $this->normalizer->denormalize(array('foo' => 'bar'), __NAMESPACE__.'\ObjectConstructorArgsWithPrivateMutatorDummy', 'any');
234+
$this->assertEquals('bar', $obj->getFoo());
235+
}
236+
231237
public function testGroupsNormalize()
232238
{
233239
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
@@ -511,15 +517,21 @@ public function testObjectToPopulate()
511517
public function testDenormalizeNonExistingAttribute()
512518
{
513519
$this->assertEquals(
514-
new PropertyDummy(),
515-
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\PropertyDummy')
520+
new GetSetDummy(),
521+
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\GetSetDummy')
516522
);
517523
}
518524

519525
public function testNoTraversableSupport()
520526
{
521527
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
522528
}
529+
530+
public function testPrivateSetter()
531+
{
532+
$obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy');
533+
$this->assertEquals('bar', $obj->getFoo());
534+
}
523535
}
524536

525537
class GetSetDummy
@@ -726,3 +738,37 @@ public function getBar_foo()
726738
return $this->bar_foo;
727739
}
728740
}
741+
742+
class ObjectConstructorArgsWithPrivateMutatorDummy
743+
{
744+
private $foo;
745+
746+
public function __construct($foo)
747+
{
748+
$this->setFoo($foo);
749+
}
750+
751+
public function getFoo()
752+
{
753+
return $this->foo;
754+
}
755+
756+
private function setFoo($foo)
757+
{
758+
$this->foo = $foo;
759+
}
760+
}
761+
762+
class ObjectWithPrivateSetterDummy
763+
{
764+
private $foo = 'bar';
765+
766+
public function getFoo()
767+
{
768+
return $this->foo;
769+
}
770+
771+
private function setFoo($foo)
772+
{
773+
}
774+
}

0 commit comments

Comments
 (0)