[Serializer] PropertyNormalizer shouldn't set static properties

This commit is contained in:
Warnar Boekkooi 2015-11-04 14:39:01 +08:00
parent e53b3b3a78
commit b15bdca96d
2 changed files with 25 additions and 1 deletions

View File

@ -50,7 +50,7 @@ class PropertyNormalizer extends AbstractNormalizer
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
foreach ($reflectionObject->getProperties() as $property) {
if (in_array($property->name, $this->ignoredAttributes)) {
if (in_array($property->name, $this->ignoredAttributes) || $property->isStatic()) {
continue;
}
@ -110,6 +110,10 @@ class PropertyNormalizer extends AbstractNormalizer
if ($allowed && !$ignored && $reflectionClass->hasProperty($propertyName)) {
$property = $reflectionClass->getProperty($propertyName);
if ($property->isStatic()) {
continue;
}
// Override visibility
if (!$property->isPublic()) {
$property->setAccessible(true);

View File

@ -409,6 +409,14 @@ class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
);
}
public function testDenormalizeShouldIgnoreStaticProperty()
{
$obj = $this->normalizer->denormalize(array('outOfScope' => true), __NAMESPACE__.'\PropertyDummy');
$this->assertEquals(new PropertyDummy(), $obj);
$this->assertEquals('out_of_scope', PropertyDummy::$outOfScope);
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
* @expectedExceptionMessage Cannot normalize attribute "bar" because injected serializer is not a normalizer
@ -429,10 +437,16 @@ class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
{
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
}
public function testNoStaticPropertySupport()
{
$this->assertFalse($this->normalizer->supportsNormalization(new StaticPropertyDummy()));
}
}
class PropertyDummy
{
public static $outOfScope = 'out_of_scope';
public $foo;
private $bar;
protected $camelCase;
@ -491,3 +505,9 @@ class PropertyCamelizedDummy
$this->kevinDunglas = $kevinDunglas;
}
}
class StaticPropertyDummy
{
private static $property = 'value';
}