From b15bdca96d143b9f3b9a78e33c9391a3a3acb8c6 Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Wed, 4 Nov 2015 14:39:01 +0800 Subject: [PATCH] [Serializer] PropertyNormalizer shouldn't set static properties --- .../Normalizer/PropertyNormalizer.php | 6 +++++- .../Normalizer/PropertyNormalizerTest.php | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php index c8e83d1f79..abbc74f27c 100644 --- a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php @@ -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); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php index 62e6d5d925..fd30381800 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php @@ -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'; +} +