8000 [Serializer] fix support for lazy properties · symfony/symfony@36ee39f · GitHub
[go: up one dir, main page]

Skip to content

Commit 36ee39f

Browse files
[Serializer] fix support for lazy properties
1 parent da00866 commit 36ee39f

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ protected function extractAttributes($object, $format = null, array $context = [
104104
}
105105

106106
// properties
107-
$propertyValues = (array) $object;
107+
$propertyValues = !method_exists($object, '__get') ? (array) $object : null;
108108
foreach ($reflClass->getProperties() as $reflProperty) {
109-
if (!\array_key_exists($reflProperty->name, $propertyValues)) {
109+
if (null !== $propertyValues && !\array_key_exists($reflProperty->name, $propertyValues)) {
110110
if ($reflProperty->isPublic()
111111
|| ($reflProperty->isProtected() && !\array_key_exists("\0*\0{$reflProperty->name}", $propertyValues))
112112
|| ($reflProperty->isPrivate() && !\array_key_exists("\0{$reflProperty->class}\0{$reflProperty->name}", $propertyValues))

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,15 @@ protected function extractAttributes($object, $format = null, array $context = [
101101
{
102102
$reflectionObject = new \ReflectionObject($object);
103103
$attributes = [];
104-
$propertyValues = (array) $object;
104+
$propertyValues = !method_exists($object, '__get') ? (array) $object : null;
105105

106106
do {
107107
foreach ($reflectionObject->getProperties() as $property) {
108-
if (($property->isPublic() && !\array_key_exists($property->name, $propertyValues))
109-
|| ($property->isProtected() && !\array_key_exists("\0*\0{$property->name}", $propertyValues))
110-
|| ($property->isPrivate() && !\array_key_exists("\0{$property->class}\0{$property->name}", $propertyValues))
108+
if ((null !== $propertyValues && (
109+
($property->isPublic() && !\array_key_exists($property->name, $propertyValues))
110+
|| ($property->isProtected() && !\array_key_exists("\0*\0{$property->name}", $propertyValues))
111+
|| ($property->isPrivate() && !\array_key_exists("\0{$property->class}\0{$property->name}", $propertyValues))
112+
))
111113
|| !$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)
112114
) {
113115
continue;

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ public function testNormalizeObjectWithUnsetProperties()
142142
);
143143
}
144144

145+
public function testNormalizeObjectWithLazyProperties()
146+
{
147+
$obj = new LazyObjectInner();
148+
unset($obj->foo);
149+
$this->assertEquals(
150+
['foo' => 123, 'bar' => null],
151+
$this->normalizer->normalize($obj, 'any')
152+
);
153+
}
154+
145155
/**
146156
* @requires PHP 7.4
147157
*/
@@ -1093,6 +1103,16 @@ class ObjectInner
10931103
public $bar;
10941104
}
10951105

1106+
class LazyObjectInner extends ObjectInner
1107+
{
1108+
public function __get($name)
1109+
{
1110+
if ('foo' === $name) {
1111+
return $this->foo = 123;
1112+
}
1113+
}
1114+
}
1115+
10961116
class FormatAndContextAwareNormalizer extends ObjectNormalizer
10971117
{
10981118
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = []): bool

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ public function testNormalizeObjectWithUnsetProperties()
111111
);
112112
}
113113

114+
public function testNormalizeObjectWithLazyProperties()
115+
{
116+
$obj = new LazyPropertyDummy();
117+
unset($obj->foo);
118+
$this->assertEquals(
119+
['foo' => 123, 'bar' => null, 'camelCase' => null],
120+
$this->normalizer->normalize($obj, 'any')
121+
);
122+
}
123+
114124
public function testDenormalize()
115125
{
116126
$obj = $this->normalizer->denormalize(
@@ -508,6 +518,16 @@ public function setCamelCase($camelCase)
508518
}
509519
}
510520

521+
class LazyPropertyDummy extends PropertyDummy
522+
{
523+
public function __get($name)
524+
{
525+
if ('foo' === $name) {
526+
return $this->foo = 123;
527+
}
528+
}
529+
}
530+
511531
class PropertyConstructorDummy
512532
{
513533
protected $foo;

0 commit comments

Comments
 (0)
0