Skip to content

Commit d6d08e6

Browse files
committed
[Serializer] Fix access to private when Ignore
1 parent 48be4b3 commit d6d08e6

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

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

Lines changed: 6 additions & 10 deletions
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

Lines changed: 19 additions & 0 deletions
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 string $foo = 'foo';
499+
500+
/** @Ignore */
501+
public string $ignored = 'ignored';
502+
503+
private string $private = 'private';
504+
}
505+
487506
class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
488507
{
489508
public function __construct()

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

Lines changed: 1 addition & 1 deletion
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)
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