Skip to content

Commit da91003

Browse files
committed
Exclude non-initialized properties accessed with getters
1 parent af8a21f commit da91003

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy