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

Skip to content

Commit 4c5286b

Browse files
Merge branch '4.4' into 5.3
* 4.4: [Serializer] fix support for unset properties on PHP < 7.4
2 parents f92f7bb + da00866 commit 4c5286b

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,17 @@ protected function extractAttributes(object $object, string $format = null, arra
107107
}
108108
}
109109

110-
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
111-
112110
// properties
111+
$propertyValues = (array) $object;
113112
foreach ($reflClass->getProperties() as $reflProperty) {
114-
$isPublic = $reflProperty->isPublic();
115-
116-
if ($checkPropertyInitialization) {
117-
if (!$isPublic) {
118-
$reflProperty->setAccessible(true);
119-
}
120-
if (!$reflProperty->isInitialized($object)) {
113+
if (!\array_key_exists($reflProperty->name, $propertyValues)) {
114+
if ($reflProperty->isPublic()
115+
|| ($reflProperty->isProtected() && !\array_key_exists("\0*\0{$reflProperty->name}", $propertyValues))
116+
|| ($reflProperty->isPrivate() && !\array_key_exists("\0{$reflProperty->class}\0{$reflProperty->name}", $propertyValues))
117+
) {
121118
unset($attributes[$reflProperty->name]);
122-
continue;
123119
}
124-
}
125120

126-
if (!$isPublic) {
127121
continue;
128122
}
129123

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 $object, string $format = null, arra
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
@@ -131,6 +131,16 @@ public function testNormalizeObjectWithUninitializedProperties()
131131
);
132132
}
133133

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ public function testNormalizeObjectWithUninitializedProperties()
9898
);
9999
}
100100

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

0 commit comments

Comments
 (0)
0