Skip to content

Commit

Permalink
[Serializer] PropertyNormalizer shouldn't set static properties
Browse files Browse the repository at this point in the history
  • Loading branch information
boekkooi committed Nov 4, 2015
1 parent e53b3b3 commit b15bdca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Expand Up @@ -50,7 +50,7 @@ public function normalize($object, $format = null, array $context = array())
$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;
}

Expand Down Expand Up @@ -110,6 +110,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
if ($allowed && !$ignored && $reflectionClass->hasProperty($propertyName)) {
$property = $reflectionClass->getProperty($propertyName);

if ($property->isStatic()) {
continue;
}

// Override visibility
if (!$property->isPublic()) {
$property->setAccessible(true);
Expand Down
Expand Up @@ -409,6 +409,14 @@ public function testDenormalizeNonExistingAttribute()
);
}

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
Expand All @@ -429,10 +437,16 @@ public function testNoTraversableSupport()
{
$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;
Expand Down Expand Up @@ -491,3 +505,9 @@ public function __construct($kevinDunglas = null)
$this->kevinDunglas = $kevinDunglas;
}
}

class StaticPropertyDummy
{
private static $property = 'value';
}

0 comments on commit b15bdca

Please sign in to comment.