Skip to content

Commit 59f357d

Browse files
committed
bug symfony#16546 [Serializer] ObjectNormalizer: don't serialize static methods and props (dunglas)
This PR was squashed before being merged into the 2.7 branch (closes symfony#16546). Discussion ---------- [Serializer] ObjectNormalizer: don't serialize static methods and props | Q | A | ------------- | --- | Bug fix? | yes | New feature? |no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#16485 | License | MIT | Doc PR | n/a Commits ------- 1fab27b [Serializer] ObjectNormalizer: don't serialize static methods and props
2 parents 0bd8b58 + 1fab27b commit 59f357d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function normalize($object, $format = null, array $context = array())
6868
$reflClass = new \ReflectionClass($object);
6969
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
7070
if (
71+
!$reflMethod->isStatic() &&
7172
!$reflMethod->isConstructor() &&
7273
!$reflMethod->isDestructor() &&
7374
0 === $reflMethod->getNumberOfRequiredParameters()
@@ -86,7 +87,9 @@ public function normalize($object, $format = null, array $context = array())
8687

8788
// properties
8889
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
89-
$attributes[$reflProperty->getName()] = true;
90+
if (!$reflProperty->isStatic()) {
91+
$attributes[$reflProperty->getName()] = true;
92+
}
9093
}
9194

9295
$attributes = array_keys($attributes);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ public function testNoTraversableSupport()
456456
{
457457
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
458458
}
459+
460+
public function testNormalizeStatic()
461+
{
462+
$this->assertEquals(array('foo' => 'K'), $this->normalizer->normalize(new ObjectWithStaticPropertiesAndMethods()));
463+
}
459464
}
460465

461466
class ObjectDummy
@@ -605,3 +610,14 @@ public function otherMethod()
605610
throw new \RuntimeException('Dummy::otherMethod() should not be called');
606611
}
607612
}
613+
614+
class ObjectWithStaticPropertiesAndMethods
615+
{
616+
public $foo = 'K';
617+
public static $bar = 'A';
618+
619+
public static function getBaz()
620+
{
621+
return 'L';
622+
}
623+
}

0 commit comments

Comments
 (0)