Skip to content

[Serializer] Fix readonly property initialization from incorrect scope #61028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
Expand Down Expand Up @@ -202,7 +203,22 @@ protected function setAttributeValue(object $object, string $attribute, mixed $v
return;
}

$reflectionProperty->setValue($object, $value);
if (!$reflectionProperty->isReadOnly()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we should deal with asymmetric visibility also?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out!

I tested the current implementation using properties with asymmetric visibility in PHP 8.4 and everything works as expected. The values are correctly set using ReflectionProperty::setValue() without throwing errors.

I believe this is because, starting from PHP 8.4.0, ReflectionProperty::setValue() still bypasses visibility restrictions (including asymmetric ones)

To make this explicit, I’ve added some tests that cover this behavior specifically for PHP 8.4.

Let me know if you'd prefer a stricter approach that checks write visibility explicitly.

$reflectionProperty->setValue($object, $value);

return;
}

if (!$reflectionProperty->isInitialized($object)) {
$declaringClass = $reflectionProperty->getDeclaringClass();
$declaringClass->getProperty($reflectionProperty->getName())->setValue($object, $value);

return;
}

if ($reflectionProperty->getValue($object) !== $value) {
throw new LogicException(\sprintf('Attempting to change readonly property "%s"::$%s.', $object::class, $reflectionProperty->getName()));
}
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Fixtures/BookDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

class BookDummy
{
public function __construct(
public private(set) string $title,
public protected(set) string $author,
protected private(set) int $pubYear,
) {
}

public function getPubYear(): int
{
return $this->pubYear;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

readonly class ChildClassDummy extends ParentClassDummy
{
public string $childProp;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

readonly class ParentClassDummy
{
private string $parentProp;

public function getParentProp(): string
{
return $this->parentProp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

class SpecialBookDummy extends BookDummy
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\GroupDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\GroupDummyChild;
use Symfony\Component\Serializer\Tests\Fixtures\ChildClassDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
use Symfony\Component\Serializer\Tests\Fixtures\SpecialBookDummy;
use Symfony\Component\Serializer\Tests\Normalizer\Features\CacheableObjectAttributesTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\CircularReferenceTestTrait;
Expand Down Expand Up @@ -174,6 +176,39 @@ public function testDenormalize()
$this->assertEquals('bar', $obj->getBar());
}

/**
* @requires PHP 8.2
*/
public function testDenormalizeWithReadOnlyClass()
{
/** @var ChildClassDummy $object */
$object = $this->normalizer->denormalize(
['parentProp' => 'parentProp', 'childProp' => 'childProp'],
ChildClassDummy::class,
'any'
);

$this->assertSame('parentProp', $object->getParentProp());
$this->assertSame('childProp', $object->childProp);
}

/**
* @requires PHP 8.4
*/
public function testDenormalizeWithAsymmetricPropertyVisibility()
{
/** @var SpecialBookDummy $object */
$object = $this->normalizer->denormalize(
['title' => 'life', 'author' => 'Santiago San Martin', 'pubYear' => 2000],
SpecialBookDummy::class,
'any'
);

$this->assertSame('life', $object->title);
$this->assertSame('Santiago San Martin', $object->author);
$this->assertSame(2000, $object->getPubYear());
}

public function testNormalizeWithParentClass()
{
$group = new GroupDummyChild();
Expand Down
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