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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Serializer] Prevent access to private properties without getters
  • Loading branch information
julienfalque committed Jan 27, 2021
commit f0409b403f2ea0a8290b417b2ae35dd0a330cec3
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,20 @@ protected function extractAttributes($object, $format = null, array $context = [

// properties
foreach ($reflClass->getProperties() as $reflProperty) {
$isPublic = $reflProperty->isPublic();

if ($checkPropertyInitialization) {
$isPublic = $reflProperty->isPublic();
if (!$isPublic) {
$reflProperty->setAccessible(true);
}
if (!$reflProperty->isInitialized($object)) {
unset($attributes[$reflProperty->name]);
continue;
}
if (!$isPublic) {
continue;
}
}

if (!$isPublic) {
continue;
}

if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;

final class DummyPrivatePropertyWithoutGetter
{
private $foo = 'foo';
private $bar = 'bar';

public function getBar()
{
return $this->bar;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\DummyPrivatePropertyWithoutGetter;
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
Expand Down Expand Up @@ -143,6 +144,15 @@ public function testNormalizeObjectWithUninitializedPrivateProperties()
);
}

public function testNormalizeObjectWithPrivatePropertyWithoutGetter()
{
$obj = new DummyPrivatePropertyWithoutGetter();
$this->assertEquals(
['bar' => 'bar'],
$this->normalizer->normalize($obj, 'any')
);
}

public function testDenormalize()
{
$obj = $this->normalizer->denormalize(
Expand Down