Skip to content

Commit 0dd2bdb

Browse files
Merge branch '4.4' into 5.0
* 4.4: (21 commits) fix merge CS [FrameworkBundle][ContainerLintCommand] Improve messages when the kernel or the container is not supported [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 [FrameworkBundle][DependencyInjection] Skip removed ids in the lint container command and its associated pass [SECURITY] Revert "AbstractAuthenticationListener.php error instead info. Rebase of symfony#28462" [FrameworkBundle][Secrets] Hook configured local dotenv file [DI] Improve performance of processDefinition fix redis multi host dsn not recognized fix constructor argument type declaration 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 ...
2 parents 5155701 + f1024c7 commit 0dd2bdb

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 $object, string $format = null, arra
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 $object, string $format = null, arra
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 <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
@@ -31,6 +31,7 @@
3131
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
3232
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
3333
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
34+
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
3435
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
3536
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
3637
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksTestTrait;
@@ -111,6 +112,18 @@ public function testNormalize()
111112
);
112113
}
113114

115+
/**
116+
* @requires PHP 7.4
117+
*/
118+
public function testNormalizeObjectWithUninitializedProperties()
119+
{
120+
$obj = new Php74Dummy();
121+
$this->assertEquals(
122+
['initializedProperty' => 'defaultValue'],
123+
$this->normalizer->normalize($obj, 'any')
124+
);
125+
}
126+
114127
public function testDenormalize()
115128
{
116129
$obj = $this->normalizer->denormalize(

Tests/Normalizer/PropertyNormalizerTest.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\Dummy;
2929
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
3030
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyChild;
31+
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
3132
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
3233
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
3334
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksTestTrait;
@@ -84,6 +85,18 @@ public function testNormalize()
8485
);
8586
}
8687

88+
/**
89+
* @requires PHP 7.4
90+
*/
91+
public function testNormalizeObjectWithUninitializedProperties()
92+
{
93+
$obj = new Php74Dummy();
94+
$this->assertEquals(
95+
['initializedProperty' => 'defaultValue'],
96+
$this->normalizer->normalize($obj, 'any')
97+
);
98+
}
99+
87100
public function testDenormalize()
88101
{
89102
$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