Skip to content

Commit

Permalink
[Serializer] fix support for lazy properties
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Nov 18, 2021
1 parent 36b95b0 commit 4c12f7d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Normalizer/ObjectNormalizer.php
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
10 changes: 6 additions & 4 deletions Normalizer/PropertyNormalizer.php
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
20 changes: 20 additions & 0 deletions Tests/Normalizer/ObjectNormalizerTest.php
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
20 changes: 20 additions & 0 deletions Tests/Normalizer/PropertyNormalizerTest.php
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

0 comments on commit 4c12f7d

Please sign in to comment.