Skip to content

Commit 6310c44

Browse files
santysisinicolas-grekas
authored andcommitted
[Serializer] Fix serializer crash due to asymmetric visibility on protected(set) properties
1 parent ece4f3e commit 6310c44

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,9 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
179179
return self::$isReadableCache[$class.$attribute];
180180
}
181181

182-
if (!isset(self::$isWritableCache[$class.$attribute])) {
183-
if (str_contains($attribute, '.')) {
184-
self::$isWritableCache[$class.$attribute] = true;
185-
} else {
186-
self::$isWritableCache[$class.$attribute] = $this->propertyInfoExtractor->isWritable($class, $attribute)
187-
|| (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType());
188-
}
189-
}
190-
191-
return self::$isWritableCache[$class.$attribute];
182+
return self::$isWritableCache[$class.$attribute] ??= str_contains($attribute, '.')
183+
|| $this->propertyInfoExtractor->isWritable($class, $attribute)
184+
|| !\in_array($this->writeInfoExtractor->getWriteInfo($class, $attribute)?->getType(), [null, PropertyWriteInfo::TYPE_NONE, PropertyWriteInfo::TYPE_PROPERTY], true);
192185
}
193186

194187
private function hasAttributeAccessorMethod(string $class, string $attribute): bool
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 <[email protected]>
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 AsymmetricVisibilityDummy
15+
{
16+
private(set) string $type;
17+
18+
public function __construct(
19+
public readonly string $item,
20+
) {
21+
$this->type = 'final';
22+
}
23+
}

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
4949
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
5050
use Symfony\Component\Serializer\Serializer;
51+
use Symfony\Component\Serializer\Tests\Fixtures\AsymmetricVisibilityDummy;
5152
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummy;
5253
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummyFirstChild;
5354
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummySecondChild;
@@ -1725,6 +1726,19 @@ public function testDenormalizationFailsWithMultipleErrorsInDefaultContext()
17251726
$this->assertSame($expected, $exceptionsAsArray);
17261727
}
17271728
}
1729+
1730+
/**
1731+
* @requires PHP 8.4
1732+
*/
1733+
public function testDeserializeObjectWithAsymmetricPropertyVisibility()
1734+
{
1735+
$serializer = new Serializer([new ObjectNormalizer()], ['json' => new JsonEncoder()]);
1736+
/** @var AsymmetricVisibilityDummy $object */
1737+
$object = $serializer->deserialize(json_encode(['type' => 'This value must not be changed because the property has a private setter', 'item' => 'one']), AsymmetricVisibilityDummy::class, 'json');
1738+
1739+
$this->assertSame('one', $object->item);
1740+
$this->assertSame('final', $object->type); // Value set in the constructor; must not be changed during deserialization
1741+
}
17281742
}
17291743

17301744
class Model

0 commit comments

Comments
 (0)