Skip to content

added ability to handle parent classes for PropertyNormalizer #24321

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected function isAllowedAttribute($classOrObject, $attribute, $format = null
}

try {
$reflectionProperty = new \ReflectionProperty(is_string($classOrObject) ? $classOrObject : get_class($classOrObject), $attribute);
$reflectionProperty = $this->getReflectionProperty($classOrObject, $attribute);
if ($reflectionProperty->isStatic()) {
return false;
}
Expand All @@ -98,13 +98,15 @@ protected function extractAttributes($object, $format = null, array $context = a
$reflectionObject = new \ReflectionObject($object);
$attributes = array();

foreach ($reflectionObject->getProperties() as $property) {
if (!$this->isAllowedAttribute($object, $property->name)) {
continue;
}
do {
foreach ($reflectionObject->getProperties() as $property) {
if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name)) {
continue;
}

$attributes[] = $property->name;
}
$attributes[] = $property->name;
}
} while ($reflectionObject = $reflectionObject->getParentClass());

return $attributes;
}
Expand All @@ -115,7 +117,7 @@ protected function extractAttributes($object, $format = null, array $context = a
protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
{
try {
$reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
$reflectionProperty = $this->getReflectionProperty($object, $attribute);
} catch (\ReflectionException $reflectionException) {
return;
}
Expand All @@ -134,7 +136,7 @@ protected function getAttributeValue($object, $attribute, $format = null, array
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
{
try {
$reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
$reflectionProperty = $this->getReflectionProperty($object, $attribute);
} catch (\ReflectionException $reflectionException) {
return;
}
Expand All @@ -150,4 +152,26 @@ protected function setAttributeValue($object, $attribute, $value, $format = null

$reflectionProperty->setValue($object, $value);
}

/**
* @param string|object $classOrObject
* @param string $attribute
*
* @return \ReflectionProperty
*
* @throws \ReflectionException
*/
private function getReflectionProperty($classOrObject, $attribute)
Copy link
Member

@dunglas dunglas Sep 28, 2017

Choose a reason for hiding this comment

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

What about:

    private function getReflectionProperty($classOrObject, $attribute)
    {
        $class = is_string($classOrObject) ? $classOrObject : get_class($classOrObject);
        while (true) {
            try {
                return isset($reflectionClass) ? $reflectionClass->getProperty($attribute) : new \ReflectionProperty($class, $attribute);
            } catch (\ReflectionException $e) {
                $reflectionClass = new \ReflectionClass($class);
                if (!$reflectionClass = $reflectionClass->getParentClass()) {
                    throw $e;
                }
            }
        }
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This runs into an endless loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would work with this:

... 
$reflectionClass = new \ReflectionClass($class);
$reflectionClass = $reflectionClass->getParentClass();
if ($reflectionClass === false) {
    throw $e;
}
...

Copy link
Member

Choose a reason for hiding this comment

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

I've updated my snippet, it should work now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i updated the PR with the snippet included

{
$reflectionClass = new \ReflectionClass($classOrObject);
while (true) {
try {
return $reflectionClass->getProperty($attribute);
} catch (\ReflectionException $e) {
if (!$reflectionClass = $reflectionClass->getParentClass()) {
throw $e;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

class GroupDummyChild extends GroupDummy
{
private $baz;

/**
* @return mixed
*/
public function getBaz()
{
return $this->baz;
}

/**
* @param mixed $baz
*/
public function setBaz($baz)
{
$this->baz = $baz;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyChild;
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
Expand Down Expand Up @@ -65,6 +66,35 @@ public function testDenormalize()
$this->assertEquals('bar', $obj->getBar());
}

public function testNormalizeWithParentClass()
{
$group = new GroupDummyChild();
$group->setBaz('baz');
$group->setFoo('foo');
$group->setBar('bar');
$group->setKevin('Kevin');
$group->setCoopTilleuls('coop');
$this->assertEquals(
array('foo' => 'foo', 'bar' => 'bar', 'kevin' => 'Kevin',
'coopTilleuls' => 'coop', 'fooBar' => null, 'symfony' => null, 'baz' => 'baz', ),
$this->normalizer->normalize($group, 'any')
);
}

public function testDenormalizeWithParentClass()
{
$obj = $this->normalizer->denormalize(
array('foo' => 'foo', 'bar' => 'bar', 'kevin' => 'Kevin', 'baz' => 'baz'),
GroupDummyChild::class,
'any'
);
$this->assertEquals('foo', $obj->getFoo());
$this->assertEquals('bar', $obj->getBar());
$this->assertEquals('Kevin', $obj->getKevin());
$this->assertEquals('baz', $obj->getBaz());
$this->assertNull($obj->getSymfony());
}

public function testConstructorDenormalize()
{
$obj = $this->normalizer->denormalize(
Expand Down Expand Up @@ -147,12 +177,14 @@ public function testGroupsNormalize()
'bar' => 'bar',
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('c'))));

// The PropertyNormalizer is not able to hydrate properties from parent classes
// The PropertyNormalizer is also able to hydrate properties from parent classes
$this->assertEquals(array(
'symfony' => 'symfony',
'foo' => 'foo',
'fooBar' => 'fooBar',
'bar' => 'bar',
'kevin' => 'kevin',
'coopTilleuls' => 'coopTilleuls',
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('a', 'c'))));
}

Expand Down