Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ed8e42

Browse files
vudaltsovfabpot
authored andcommitted
[Serializer] Skip uninitialized (PHP 7.4) properties in PropertyNormalizer and ObjectNormalizer
1 parent 3b42ca9 commit 1ed8e42

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

src/Symfony/Component/Serializer/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
}

src/Symfony/Component/Serializer/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
}
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 <[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+
/**
15+
* @author Valentin Udaltsov <[email protected]>
16+
*/
17+
final class Php74Dummy
18+
{
19+
public string $uninitializedProperty;
20+
21+
public string $initializedProperty = 'defaultValue';
22+
}

src/Symfony/Component/Serializer/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(

src/Symfony/Component/Serializer/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)