Skip to content

Commit

Permalink
Merge branch '4.4' into 5.3
Browse files Browse the repository at this point in the history
* 4.4:
  [Serializer] fix support for lazy properties
  • Loading branch information
nicolas-grekas committed Nov 18, 2021
2 parents 54489a9 + 4c12f7d commit 3fd70a2
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 @@ -108,9 +108,9 @@ protected function extractAttributes(object $object, string $format = null, arra
}

// 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 $object, string $format = null, arra
{
$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 @@ -141,6 +141,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 @@ -938,6 +948,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, string $attribute, string $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 @@ -108,6 +108,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 @@ -441,6 +451,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 3fd70a2

Please sign in to comment.