8000 [Serializer] fix support for unset properties on PHP < 7.4 · symfony/symfony@b103717 · GitHub
[go: up one dir, main page]

Skip to content

Commit b103717

Browse files
[Serializer] fix support for unset properties on PHP < 7.4
1 parent 7d218cc commit b103717

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,17 @@ protected function extractAttributes($object, $format = null, array $context = [
103103
}
104104
}
105105

106-
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
107-
108106
// properties
107+
$propertyValues = (array) $object;
109108
foreach ($reflClass->getProperties() as $reflProperty) {
110-
$isPublic = $reflProperty->isPublic();
111-
112-
if ($checkPropertyInitialization) {
113-
if (!$isPublic) {
114-
$reflProperty->setAccessible(true);
115-
}
116-
if (!$reflProperty->isInitialized($object)) {
109+
if (!\array_key_exists($reflProperty->name, $propertyValues)) {
110+
if ($reflProperty->isPublic()
111+
|| ($reflProperty->isProtected() && !\array_key_exists("\0*\0{$reflProperty->name}", $propertyValues))
112+
|| ($reflProperty->isPrivate() && !\array_key_exists("\0{$reflProperty->class}\0{$reflProperty->name}", $propertyValues))
113+
) {
117114
unset($attributes[$reflProperty->name]);
118-
continue;
119115
}
120-
}
121116

122-
if (!$isPublic) {
123117
continue;
124118
}
125119

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,15 @@ protected function extractAttributes($object, $format = null, array $context = [
101101
{
102102
$reflectionObject = new \ReflectionObject($object);
103103
$attributes = [];
104-
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
104+
$propertyValues = (array) $object;
105105

106106
do {
107107
foreach ($reflectionObject->getProperties() as $property) {
108-
if ($checkPropertyInitialization) {
109-
if (!$property->isPublic()) {
110-
$property->setAccessible(true);
111-
}
112-
113-
if (!$property->isInitialized($object)) {
114-
continue;
115-
}
116-
}
117-
118-
if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)) {
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))
111+
|| !$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)
112+
) {
119113
continue;
120114
}
121115

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ public function testNormalizeObjectWithUninitializedProperties()
132132
);
133133
}
134134

135+
public function testNormalizeObjectWithUnsetProperties()
136+
{
137+
$obj = new ObjectInner();
138+
unset($obj->foo);
139+
$this->assertEquals(
140+
['bar' => null],
141+
$this->normalizer->normalize($obj, 'any')
142+
);
143+
}
144+
135145
/**
136146
* @requires PHP 7.4
137147
*/

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ public function testNormalizeObjectWithUninitializedProperties()
101101
);
102102
}
103103

104+
public function testNormalizeObjectWithUnsetProperties()
105+
{
106+
$obj = new PropertyDummy();
107+
unset($obj->foo);
108+
$this->assertEquals(['bar' => null, 'camelCase' => null], $this->normalizer->normalize($obj, 'any'));
109+
}
110+
104111
public function testDenormalize()
105112
{
106113
$obj = $this->normalizer->denormalize(

0 commit comments

Comments
 (0)
0