Skip to content

[Serializer] PropertyNormalizer shouldn't set static properties #16453

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

Merged
merged 1 commit into from
Nov 5, 2015
Merged
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 @@ -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
Original file line number Diff line number Diff line change
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';
}