Skip to content

Commit 1da3a5c

Browse files
bug symfony#52680 [Serializer] Fix access to private properties/getters when using the @Ignore annotation (mtarld)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix access to private properties/getters when using the ``@Ignore`` annotation | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#52673 symfony#49710 | License | MIT Commits ------- cc356b0 [Serializer] Fix access to private when Ignore
2 parents cd98a3f + cc356b0 commit 1da3a5c

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -302,22 +302,18 @@ protected function getAttributes(object $object, ?string $format, array $context
302302
return $this->attributesCache[$key];
303303
}
304304

305-
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
306-
307-
if (false !== $allowedAttributes) {
308-
if ($context['cache_key']) {
309-
$this->attributesCache[$key] = $allowedAttributes;
310-
}
311-
312-
return $allowedAttributes;
313-
}
314-
315305
$attributes = $this->extractAttributes($object, $format, $context);
316306

317307
if ($this->classDiscriminatorResolver && $mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object)) {
318308
array_unshift($attributes, $mapping->getTypeProperty());
319309
}
320310

311+
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
312+
313+
if (false !== $allowedAttributes) {
314+
$attributes = array_intersect($attributes, $allowedAttributes);
315+
}
316+
321317
if ($context['cache_key'] && \stdClass::class !== $class) {
322318
$this->attributesCache[$key] = $attributes;
323319
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1717
use Symfony\Component\PropertyInfo\Type;
18+
use Symfony\Component\Serializer\Annotation\Ignore;
1819
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
1920
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
2021
use Symfony\Component\Serializer\Exception\LogicException;
@@ -444,6 +445,14 @@ public function testNormalizeEmptyObject()
444445
$normalizedData = $normalizer->normalize(new EmptyDummy(), 'any', ['preserve_empty_objects' => true]);
445446
$this->assertEquals(new \ArrayObject(), $normalizedData);
446447
}
448+
449+
public function testNormalizeWithIgnoreAnnotationAndPrivateProperties()
450+
{
451+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
452+
$serializer = new Serializer([new ObjectNormalizer($classMetadataFactory)]);
453+
454+
$this->assertSame(['foo' => 'foo'], $serializer->normalize(new ObjectDummyWithIgnoreAnnotationAndPrivateProperty()));
455+
}
447456
}
448457

449458
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -484,6 +493,16 @@ class EmptyDummy
484493
{
485494
}
486495

496+
class ObjectDummyWithIgnoreAnnotationAndPrivateProperty
497+
{
498+
public $foo = 'foo';
499+
500+
/** @Ignore */
501+
public $ignored = 'ignored';
502+
503+
private $private = 'private';
504+
}
505+
487506
class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
488507
{
489508
public function __construct()

src/Symfony/Component/Serializer/Tests/SerializerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ public function testDeserializeAndSerializeInterfacedObjectsWithTheClassMetadata
471471
'groups' => ['two'],
472472
]);
473473

474-
$this->assertEquals('{"two":2,"type":"one"}', $serialized);
474+
$this->assertEquals('{"type":"one","two":2}', $serialized);
475475
}
476476

477477
public function testDeserializeAndSerializeNestedInterfacedObjectsWithTheClassMetadataDiscriminator()

0 commit comments

Comments
 (0)