8000 bug #16453 [Serializer] PropertyNormalizer shouldn't set static prope… · symfony/symfony@e21bf56 · GitHub
[go: up one dir, main page]

Skip to content

Commit e21bf56

Browse files
committed
bug #16453 [Serializer] PropertyNormalizer shouldn't set static properties (boekkooi)
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] PropertyNormalizer shouldn't set static properties | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR fixes a bug where static properties where set by the PropertyNormalizer. Commits ------- b15bdca [Serializer] PropertyNormalizer shouldn't set static properties
2 parents a3f2f5c + b15bdca commit e21bf56

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function normalize($object, $format = null, array $context = array())
5050
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
5151

5252
foreach ($reflectionObject->getProperties() as $property) {
53-
if (in_array($property->name, $this->ignoredAttributes)) {
53+
if (in_array($property->name, $this->ignoredAttributes) || $property->isStatic()) {
5454
continue;
5555
}
5656

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

113+
if ($property->isStatic()) {
114+
continue;
115+
}
116+
113117
// Override visibility
114118
if (!$property->isPublic()) {
115119
$property->setAccessible(true);

src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ public function testDenormalizeNonExistingAttribute()
409409
);
410410
}
411411

412+
public function testDenormalizeShouldIgnoreStaticProperty()
413+
{
414+
$obj = $this->normalizer->denormalize(array('outOfScope' => true), __NAMESPACE__.'\PropertyDummy');
415+
416+
$this->assertEquals(new PropertyDummy(), $obj);
417+
$this->assertEquals('out_of_scope', PropertyDummy::$outOfScope);
418+
}
419+
412420
/**
413421
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
414422
* @expectedExceptionMessage Cannot normalize attribute "bar" because injected serializer is not a normalizer
@@ -429,10 +437,16 @@ public function testNoTraversableSupport()
429437
{
430438
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
431439
}
440+
441+
public function testNoStaticPropertySupport()
442+
{
443+
$this->assertFalse($this->normalizer->supportsNormalization(new StaticPropertyDummy()));
444+
}
432445
}
433446

434447
class PropertyDummy
435448
{
449+
public static $outOfScope = 'out_of_scope';
436450
public $foo;
437451
private $bar;
438452
protected $camelCase;
@@ -491,3 +505,9 @@ public function __construct($kevinDunglas = null)
491505
$this->kevinDunglas = $kevinDunglas;
492506
}
493507
}
508+
509+
class StaticPropertyDummy
510+
{
511+
private static $property = 'value';
512+
}
513+

0 commit comments

Comments
 (0)
0