Exclude non-initialized properties accessed with getters
This commit is contained in:
parent
af8a21f0af
commit
da9100320e
@ -106,9 +106,19 @@ class ObjectNormalizer extends AbstractObjectNormalizer
|
|||||||
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
|
$checkPropertyInitialization = \PHP_VERSION_ID >= 70400;
|
||||||
|
|
||||||
// properties
|
// properties
|
||||||
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
|
foreach ($reflClass->getProperties() as $reflProperty) {
|
||||||
if ($checkPropertyInitialization && !$reflProperty->isInitialized($object)) {
|
if ($checkPropertyInitialization) {
|
||||||
continue;
|
$isPublic = $reflProperty->isPublic();
|
||||||
|
if (!$isPublic) {
|
||||||
|
$reflProperty->setAccessible(true);
|
||||||
|
}
|
||||||
|
if (!$reflProperty->isInitialized($object)) {
|
||||||
|
unset($attributes[$reflProperty->name]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!$isPublic) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
|
if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
<?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 Alexander Borisov <boshurik@gmail.com>
|
||||||
|
*/
|
||||||
|
final class Php74DummyPrivate
|
||||||
|
{
|
||||||
|
private string $uninitializedProperty;
|
||||||
|
|
||||||
|
private string $initializedProperty = 'defaultValue';
|
||||||
|
|
||||||
|
public function getUninitializedProperty(): string
|
||||||
|
{
|
||||||
|
return $this->uninitializedProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getInitializedProperty(): string
|
||||||
|
{
|
||||||
|
return $this->initializedProperty;
|
||||||
|
}
|
||||||
|
}
|
@ -34,6 +34,7 @@ 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\OtherSerializedNameDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
|
||||||
|
use Symfony\Component\Serializer\Tests\Fixtures\Php74DummyPrivate;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
|
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
|
||||||
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
|
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
|
||||||
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksObject;
|
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksObject;
|
||||||
@ -127,6 +128,18 @@ class ObjectNormalizerTest extends TestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 7.4
|
||||||
|
*/
|
||||||
|
public function testNormalizeObjectWithUninitializedPrivateProperties()
|
||||||
|
{
|
||||||
|
$obj = new Php74DummyPrivate();
|
||||||
|
$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