8000 Merge branch '4.4' into 5.3 · symfony/symfony@3bd4f57 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3bd4f57

Browse files
Merge branch '4.4' into 5.3
* 4.4: [Serializer] fix support for lazy properties
2 parents 86deb3c + 17e5244 commit 3bd4f57

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
@@ -108,9 +108,9 @@ protected function extractAttributes(object $object, string $format = null, arra
108108
}
109109

110110
// properties
111-
$propertyValues = (array) $object;
111+
$propertyValues = !method_exists($object, '__get') ? (array) $object : null;
112112
foreach ($reflClass->getProperties() as $reflProperty) {
113-
if (!\array_key_exists($reflProperty->name, $propertyValues)) {
113+
if (null !== $propertyValues && !\array_key_exists($reflProperty->name, $propertyValues)) {
114114
if ($reflProperty->isPublic()
115115
|| ($reflProperty->isProtected() && !\array_key_exists("\0*\0{$reflProperty->name}", $propertyValues))
116116
|| ($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 $object, string $format = null, arra
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
@@ -141,6 +141,16 @@ public function testNormalizeObjectWithUnsetProperties()
141141
);
142142
}
143143

144+
public function testNormalizeObjectWithLazyProperties()
145+
{
146+
$obj = new LazyObjectInner();
147+
unset($obj->foo);
148+
$this->assertEquals(
149+
['foo' => 123, 'bar' => null],
150+
$this->normalizer->normalize($obj, 'any')
151+
);
152+
}
153+
144154
/**< 10000 /div>
145155
* @requires PHP 7.4
146156
*/
@@ -938,6 +948,16 @@ class ObjectInner
938948
public $bar;
939949
}
940950

951+
class LazyObjectInner extends ObjectInner
952+
{
953+
public function __get($name)
954+
{
955+
if ('foo' === $name) {
956+
return $this->foo = 123;
957+
}
958+
}
959+
}
960+
941961
class FormatAndContextAwareNormalizer extends ObjectNormalizer
942962
{
943963
protected function isAllowedAttribute($classOrObject, string $attribute, string $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
@@ -108,6 +108,16 @@ public function testNormalizeObjectWithUnsetProperties()
108108
);
109109
}
110110

111+
public function testNormalizeObjectWithLazyProperties()
112+
{
113+
$obj = new LazyPropertyDummy();
114+
unset($obj->foo);
115+
$this->assertEquals(
116+
['foo' => 123, 'bar' => null, 'camelCase' => null],
117+
$this->normalizer->normalize($obj, 'any')
118+
);
119+
}
120+
111121
public function testDenormalize()
112122
{
113123
$obj = $this->normalizer->denormalize(
@@ -441,6 +451,16 @@ public function setCamelCase($camelCase)
441451
}
442452
}
443453

454+
class LazyPropertyDummy extends PropertyDummy
455+
{
456+
public function __get($name)
457+
{
458+
if ('foo' === $name) {
459+
return $this->foo = 123;
460+
}
461+
}
462+
}
463+
444464
class PropertyConstructorDummy
445465
{
446466
protected $foo;

0 commit comments

Comments
 (0)
0