Skip to content

Commit 8533ea2

Browse files
bug #40004 [Serializer] Prevent access to private properties without getters (julienfalque)
This PR was merged into the 4.4 branch. Discussion ---------- [Serializer] Prevent access to private properties without getters | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - When upgrading `symfony/serializer` from `v5.2.1` to `v5.2.2`, the serializer starts throwing exceptions because it cannot access some private properties that don't have a getter. This looks related to #38900. Commits ------- f0409b4 [Serializer] Prevent access to private properties without getters
2 parents f667d79 + f0409b4 commit 8533ea2

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,20 @@ protected function extractAttributes($object, $format = null, array $context = [
107107

108108
// properties
109109
foreach ($reflClass->getProperties() as $reflProperty) {
110+
$isPublic = $reflProperty->isPublic();
111+
110112
if ($checkPropertyInitialization) {
111-
$isPublic = $reflProperty->isPublic();
112113
if (!$isPublic) {
113114
$reflProperty->setAccessible(true);
114115
}
115116
if (!$reflProperty->isInitialized($object)) {
116117
unset($attributes[$reflProperty->name]);
117118
continue;
118119
}
119-
if (!$isPublic) {
120-
continue;
121-
}
120+
}
121+
122+
if (!$isPublic) {
123+
continue;
122124
}
123125

124126
if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
final class DummyPrivatePropertyWithoutGetter
15+
{
16+
private $foo = 'foo';
17+
private $bar = 'bar';
18+
19+
public function getBar()
20+
{
21+
return $this->bar;
22+
}
23+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Component\Serializer\Serializer;
3434
use Symfony\Component\Serializer\SerializerInterface;
3535
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
36+
use Symfony\Component\Serializer\Tests\Fixtures\DummyPrivatePropertyWithoutGetter;
3637
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
3738
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
3839
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
@@ -143,6 +144,15 @@ public function testNormalizeObjectWithUninitializedPrivateProperties()
143144
);
144145
}
145146

147+
public function testNormalizeObjectWithPrivatePropertyWithoutGetter()
148+
{
149+
$obj = new DummyPrivatePropertyWithoutGetter();
150+
$this->assertEquals(
151+
['bar' => 'bar'],
152+
$this->normalizer->normalize($obj, 'any')
153+
);
154+
}
155+
146156
public function testDenormalize()
147157
{
148158
$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