[Serializer] PropertyNormalizer shouldn't set static properties
This commit is contained in:
parent
e53b3b3a78
commit
b15bdca96d
@ -50,7 +50,7 @@ class PropertyNormalizer extends AbstractNormalizer
|
|||||||
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
|
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
|
||||||
|
|
||||||
foreach ($reflectionObject->getProperties() as $property) {
|
foreach ($reflectionObject->getProperties() as $property) {
|
||||||
if (in_array($property->name, $this->ignoredAttributes)) {
|
if (in_array($property->name, $this->ignoredAttributes) || $property->isStatic()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,6 +110,10 @@ class PropertyNormalizer extends AbstractNormalizer
|
|||||||
if ($allowed && !$ignored && $reflectionClass->hasProperty($propertyName)) {
|
if ($allowed && !$ignored && $reflectionClass->hasProperty($propertyName)) {
|
||||||
$property = $reflectionClass->getProperty($propertyName);
|
$property = $reflectionClass->getProperty($propertyName);
|
||||||
|
|
||||||
|
if ($property->isStatic()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Override visibility
|
// Override visibility
|
||||||
if (!$property->isPublic()) {
|
if (!$property->isPublic()) {
|
||||||
$property->setAccessible(true);
|
$property->setAccessible(true);
|
||||||
|
@ -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
|
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
|
||||||
* @expectedExceptionMessage Cannot normalize attribute "bar" because injected serializer is not a normalizer
|
* @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()));
|
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNoStaticPropertySupport()
|
||||||
|
{
|
||||||
|
$this->assertFalse($this->normalizer->supportsNormalization(new StaticPropertyDummy()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PropertyDummy
|
class PropertyDummy
|
||||||
{
|
{
|
||||||
|
public static $outOfScope = 'out_of_scope';
|
||||||
public $foo;
|
public $foo;
|
||||||
private $bar;
|
private $bar;
|
||||||
protected $camelCase;
|
protected $camelCase;
|
||||||
@ -491,3 +505,9 @@ class PropertyCamelizedDummy
|
|||||||
$this->kevinDunglas = $kevinDunglas;
|
$this->kevinDunglas = $kevinDunglas;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class StaticPropertyDummy
|
||||||
|
{
|
||||||
|
private static $property = 'value';
|
||||||
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user