Skip to content

[Serializer] fix support for lazy properties #44121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ protected function extractAttributes($object, $format = null, array $context = [
}

// properties
$propertyValues = (array) $object;
$propertyValues = !method_exists($object, '__get') ? (array) $object : null;
foreach ($reflClass->getProperties() as $reflProperty) {
if (!\array_key_exists($reflProperty->name, $propertyValues)) {
if (null !== $propertyValues && !\array_key_exists($reflProperty->name, $propertyValues)) {
if ($reflProperty->isPublic()
|| ($reflProperty->isProtected() && !\array_key_exists("\0*\0{$reflProperty->name}", $propertyValues))
|| ($reflProperty->isPrivate() && !\array_key_exists("\0{$reflProperty->class}\0{$reflProperty->name}", $propertyValues))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ protected function extractAttributes($object, $format = null, array $context = [
{
$reflectionObject = new \ReflectionObject($object);
$attributes = [];
$propertyValues = (array) $object;
$propertyValues = !method_exists($object, '__get') ? (array) $object : null;

do {
foreach ($reflectionObject->getProperties() as $property) {
if (($property->isPublic() && !\array_key_exists($property->name, $propertyValues))
|| ($property->isProtected() && !\array_key_exists("\0*\0{$property->name}", $propertyValues))
|| ($property->isPrivate() && !\array_key_exists("\0{$property->class}\0{$property->name}", $propertyValues))
if ((null !== $propertyValues && (
($property->isPublic() && !\array_key_exists($property->name, $propertyValues))
|| ($property->isProtected() && !\array_key_exists("\0*\0{$property->name}", $propertyValues))
|| ($property->isPrivate() && !\array_key_exists("\0{$property->class}\0{$property->name}", $propertyValues))
))
|| !$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)
) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ public function testNormalizeObjectWithUnsetProperties()
);
}

public function testNormalizeObjectWithLazyProperties()
{
$obj = new LazyObjectInner();
unset($obj->foo);
$this->assertEquals(
['foo' => 123, 'bar' => null],
$this->normalizer->normalize($obj, 'any')
);
}

/**
* @requires PHP 7.4
*/
Expand Down Expand Up @@ -1093,6 +1103,16 @@ class ObjectInner
public $bar;
}

class LazyObjectInner extends ObjectInner
{
public function __get($name)
{
if ('foo' === $name) {
return $this->foo = 123;
}
}
}

class FormatAndContextAwareNormalizer extends ObjectNormalizer
{
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = []): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ public function testNormalizeObjectWithUnsetProperties()
);
}

public function testNormalizeObjectWithLazyProperties()
{
$obj = new LazyPropertyDummy();
unset($obj->foo);
$this->assertEquals(
['foo' => 123, 'bar' => null, 'camelCase' => null],
$this->normalizer->normalize($obj, 'any')
);
}

public function testDenormalize()
{
$obj = $this->normalizer->denormalize(
Expand Down Expand Up @@ -508,6 +518,16 @@ public function setCamelCase($camelCase)
}
}

class LazyPropertyDummy extends PropertyDummy
{
public function __get($name)
{
if ('foo' === $name) {
return $this->foo = 123;
}
}
}

class PropertyConstructorDummy
{
protected $foo;
Expand Down