8000 bug #38900 [Serializer] Exclude non-initialized properties accessed w… · symfony/symfony@484a95d · GitHub
[go: up one dir, main page]

Skip to content

Commit 484a95d

Browse files
bug #38900 [Serializer] Exclude non-initialized properties accessed with getters (BoShurik)
This PR was merged into the 4.4 branch. Discussion ---------- [Serializer] Exclude non-initialized properties accessed with getters | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | no <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Allow to serialize ```php final class Php74DummyPrivate { private string $uninitializedProperty; private string $initializedProperty = 'defaultValue'; public function getUninitializedProperty(): string { return $this->uninitializedProperty; } public function getInitializedProperty(): string { return $this->initializedProperty; } } ``` Similar to #34791 Commits ------- da91003 Exclude non-initialized properties accessed with getters
2 parents ffdc46c + da91003 commit 484a95d

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,19 @@ protected function extractAttributes($object, $format = null, array $context = [
106106
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
107107

108108
// properties
109-
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
110-
if ($checkPropertyInitialization && !$reflProperty->isInitialized($object)) {
111-
continue;
109+
foreach ($reflClass->getProperties() as $reflProperty) {
110+
if ($checkPropertyInitialization) {
111+
$isPublic = $reflProperty->isPublic();
112+
if (!$isPublic) {
113+
$reflProperty->setAccessible(true);
114+
}
115+
if (!$reflProperty->isInitialized($object)) {
116+
unset($attributes[$reflProperty->name]);
117+
continue;
118+
}
119+
if (!$isPublic) {
120+
continue;
121+
}
112122
}
113123

114124
if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
/**
15+
* @author Alexander Borisov <boshurik@gmail.com>
16+
*/
17+
final class Php74DummyPrivate
18+
{
19+
private string $uninitializedProperty;
20+
21+
private string $initializedProperty = 'defaultValue';
22+
23+
public function getUninitializedProperty(): string
24+
{
25+
return $this->uninitializedProperty;
26+
}
27+
28+
public function getInitializedProperty(): string
29+
{
30+
return $this->initializedProperty;
31+
}
32+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
3535
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
3636
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
37+
use Symfony\Component\Serializer\Tests\Fixtures\Php74DummyPrivate;
3738
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
3839
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
3940
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksObject;
@@ -127,6 +128,18 @@ public function testNormalizeObjectWithUninitializedProperties()
127128
);
128129
}
129130

131+
/**
132+
* @requires PHP 7.4
133+
*/
134+
public function testNormalizeObjectWithUninitializedPrivateProperties()
135+
{
136+
$obj = new Php74DummyPrivate();
137+
$this->assertEquals(
138+
['initializedProperty' => 'defaultValue'],
139+
$this->normalizer->normalize($obj, 'any')
140+
);
141+
}
142+
130143
public function testDenormalize()
131144
{
132145
$obj = $this->normalizer->denormalize(

0 commit comments

Comments
 (0)
0