bug #34791 [Serializer] Skip uninitialized (PHP 7.4) properties in PropertyNormalizer and ObjectNormalizer (vudaltsov)
This PR was squashed before being merged into the 3.4 branch (closes #34791).
Discussion
----------
[Serializer] Skip uninitialized (PHP 7.4) properties in PropertyNormalizer and ObjectNormalizer
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | n/a
| License | MIT
| Doc PR | n/a
When trying to read from an uninitialized property in PHP 7.4, a `TypeError` is generated, see https://wiki.php.net/rfc/typed_properties_v2#uninitialized_and_unset_properties. This PR fixes the issue.
Commits
-------
1ed8e42d15
[Serializer] Skip uninitialized (PHP 7.4) properties in PropertyNormalizer and ObjectNormalizer
This commit is contained in:
commit
8f42a7679d
@ -83,8 +83,14 @@ class ObjectNormalizer extends AbstractObjectNormalizer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
|
||||||
|
|
||||||
// properties
|
// properties
|
||||||
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
|
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
|
||||||
|
if ($checkPropertyInitialization && !$reflProperty->isInitialized($object)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
|
if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -99,9 +99,20 @@ class PropertyNormalizer extends AbstractObjectNormalizer
|
|||||||
{
|
{
|
||||||
$reflectionObject = new \ReflectionObject($object);
|
$reflectionObject = new \ReflectionObject($object);
|
||||||
$attributes = [];
|
$attributes = [];
|
||||||
|
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
foreach ($reflectionObject->getProperties() as $property) {
|
foreach ($reflectionObject->getProperties() as $property) {
|
||||||
|
if ($checkPropertyInitialization) {
|
||||||
|
if (!$property->isPublic()) {
|
||||||
|
$property->setAccessible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$property->isInitialized($object)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)) {
|
if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Valentin Udaltsov <udaltsov.valentin@gmail.com>
|
||||||
|
*/
|
||||||
|
final class Php74Dummy
|
||||||
|
{
|
||||||
|
public string $uninitializedProperty;
|
||||||
|
|
||||||
|
public string $initializedProperty = 'defaultValue';
|
||||||
|
}
|
@ -28,6 +28,7 @@ use Symfony\Component\Serializer\SerializerInterface;
|
|||||||
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
|
||||||
|
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
|
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -81,6 +82,18 @@ class ObjectNormalizerTest extends TestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 7.4
|
||||||
|
*/
|
||||||
|
public function testNormalizeObjectWithUninitializedProperties()
|
||||||
|
{
|
||||||
|
$obj = new Php74Dummy();
|
||||||
|
$this->assertEquals(
|
||||||
|
['initializedProperty' => 'defaultValue'],
|
||||||
|
$this->normalizer->normalize($obj, 'any')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testDenormalize()
|
public function testDenormalize()
|
||||||
{
|
{
|
||||||
$obj = $this->normalizer->denormalize(
|
$obj = $this->normalizer->denormalize(
|
||||||
|
@ -22,6 +22,7 @@ use Symfony\Component\Serializer\SerializerInterface;
|
|||||||
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyChild;
|
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyChild;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
|
||||||
|
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
|
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
|
||||||
|
|
||||||
@ -55,6 +56,18 @@ class PropertyNormalizerTest extends TestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 7.4
|
||||||
|
*/
|
||||||
|
public function testNormalizeObjectWithUninitializedProperties()
|
||||||
|
{
|
||||||
|
$obj = new Php74Dummy();
|
||||||
|
$this->assertEquals(
|
||||||
|
['initializedProperty' => 'defaultValue'],
|
||||||
|
$this->normalizer->normalize($obj, 'any')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testDenormalize()
|
public function testDenormalize()
|
||||||
{
|
{
|
||||||
$obj = $this->normalizer->denormalize(
|
$obj = $this->normalizer->denormalize(
|
||||||
|
Reference in New Issue
Block a user