Skip to content

Commit f1024c7

Browse files
Merge branch '4.3' into 4.4
* 4.3: fix merge CS [Serializer] Skip uninitialized (PHP 7.4) properties in PropertyNormalizer and ObjectNormalizer stop using deprecated Doctrine persistence classes [Cache] Fix wrong classname in deprecation message Fix regex lookahead syntax in ApplicationTest Fixed syntax in comment [SecurityBundle][FirewallMap] Remove unused property [Messenger][AMQP] Use delivery_mode=2 by default [DI] Improve performance of processDefinition Fix invalid Windows path normalization [Validator][ConstraintValidator] Safe fail on invalid timezones [DoctrineBridge] Fixed submitting invalid ids when using queries with limit [FrameworkBundle] Add info & example to auto_mapping config fix comparisons with null values at property paths
2 parents 21641cc + b085f86 commit f1024c7

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
@@ -103,8 +103,14 @@ protected function extractAttributes($object, $format = null, array $context = [
103103
}
104104
}
105105

106+
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
107+
106108
// properties
107109
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
110+
if ($checkPropertyInitialization && !$reflProperty->isInitialized($object)) {
111+
continue;
112+
}
113+
108114
if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
109115
continue;
110116
}

Normalizer/PropertyNormalizer.php

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

105106
do {
106107
foreach ($reflectionObject->getProperties() as $property) {
108+
if ($checkPropertyInitialization) {
109+
if (!$property->isPublic()) {
110+
$property->setAccessible(true);
111+
}
112+
113+
if (!$property->isInitialized($object)) {
114+
continue;
115+
}
116+
}
117+
107118
if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)) {
108119
continue;
109120
}

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 <[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+
}

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
3434
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
3535
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
36+
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
3637
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
3738
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
3839
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksObject;
@@ -114,6 +115,18 @@ public function testNormalize()
114115
);
115116
}
116117

118+
/**
119+
* @requires PHP 7.4
120+
*/
121+
public function testNormalizeObjectWithUninitializedProperties()
122+
{
123+
$obj = new Php74Dummy();
124+
$this->assertEquals(
125+
['initializedProperty' => 'defaultValue'],
126+
$this->normalizer->normalize($obj, 'any')
127+
);
128+
}
129+
117130
public function testDenormalize()
118131
{
119132
$obj = $this->normalizer->denormalize(

Tests/Normalizer/PropertyNormalizerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
3131
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
3232
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyChild;
33+
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
3334
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
3435
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
3536
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksObject;
@@ -87,6 +88,18 @@ public function testNormalize()
8788
);
8889
}
8990

91+
/**
92+
* @requires PHP 7.4
93+
*/
94+
public function testNormalizeObjectWithUninitializedProperties()
95+
{
96+
$obj = new Php74Dummy();
97+
$this->assertEquals(
98+
['initializedProperty' => 'defaultValue'],
99+
$this->normalizer->normalize($obj, 'any')
100+
);
101+
}
102+
90103
public function testDenormalize()
91104
{
92105
$obj = $this->normalizer->denormalize(

0 commit comments

Comments
 (0)