Skip to content

Commit f7244c6

Browse files
committed
bug symfony#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 symfony#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 ------- 1ed8e42 [Serializer] Skip uninitialized (PHP 7.4) properties in PropertyNormalizer and ObjectNormalizer
2 parents fd90499 + 33121ae commit f7244c6

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

Normalizer/ObjectNormalizer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,14 @@ protected function extractAttributes($object, $format = null, array $context = [
8383
}
8484
}
8585

86+
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
87+
8688
// properties
8789
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
90+
if ($checkPropertyInitialization && !$reflProperty->isInitialized($object)) {
91+
continue;
92+
}
93+
8894
if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
8995
continue;
9096
}

Normalizer/PropertyNormalizer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,20 @@ protected function extractAttributes($object, $format = null, array $context = [
9999
{
100100
$reflectionObject = new \ReflectionObject($object);
101101
$attributes = [];
102+
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
102103

103104
do {
104105
foreach ($reflectionObject->getProperties() as $property) {
106+
if ($checkPropertyInitialization) {
107+
if (!$property->isPublic()) {
108+
$property->setAccessible(true);
109+
}
110+
111+
if (!$property->isInitialized($object)) {
112+
continue;
113+
}
114+
}
115+
105116
if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)) {
106117
continue;
107118
}

Tests/Fixtures/Php74Dummy.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 Valentin Udaltsov <udaltsov.valentin@gmail.com>
16+
*/
17+
final class Php74Dummy
18+
{
19+
public string $uninitializedProperty;
20+
21+
public string $initializedProperty = 'defaultValue';
22+
}

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
2929
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
3030
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
31+
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
3132
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
3233

3334
/**
@@ -81,6 +82,18 @@ public function testNormalize()
8182
);
8283
}
8384

85+
/**
86+
* @requires PHP 7.4
87+
*/
88+
public function testNormalizeObjectWithUninitializedProperties()
89+
{
90+
$obj = new Php74Dummy();
91+
$this->assertEquals(
92+
['initializedProperty' => 'defaultValue'],
93+
$this->normalizer->normalize($obj, 'any')
94+
);
95+
}
96+
8497
public function testDenormalize()
8598
{
8699
$obj = $this->normalizer->denormalize(

Tests/Normalizer/PropertyNormalizerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
2323
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyChild;
2424
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
25+
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
2526
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
2627
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
2728

@@ -55,6 +56,18 @@ public function testNormalize()
5556
);
5657
}
5758

59+
/**
60+
* @requires PHP 7.4
61+
*/
62+
public function testNormalizeObjectWithUninitializedProperties()
63+
{
64+
$obj = new Php74Dummy();
65+
$this->assertEquals(
66+
['initializedProperty' => 'defaultValue'],
67+
$this->normalizer->normalize($obj, 'any')
68+
);
69+
}
70+
5871
public function testDenormalize()
5972
{
6073
$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