Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1fab27b

Browse filesBrowse files
dunglasfabpot
authored andcommitted
[Serializer] ObjectNormalizer: don't serialize static methods and props
1 parent 0bd8b58 commit 1fab27b
Copy full SHA for 1fab27b

File tree

2 files changed

+20
-1
lines changed
Filter options

2 files changed

+20
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
+4-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+16Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.