Skip to content

Commit

Permalink
bug #52680 [Serializer] Fix access to private properties/getters when…
Browse files Browse the repository at this point in the history
… 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 #52673 #49710
| License       | MIT

Commits
-------

cc356b0 [Serializer] Fix access to private when Ignore
  • Loading branch information
nicolas-grekas committed Nov 24, 2023
2 parents cd98a3f + cc356b0 commit 1da3a5c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Expand Up @@ -302,22 +302,18 @@ protected function getAttributes(object $object, ?string $format, array $context
return $this->attributesCache[$key];
}

$allowedAttributes = $this->getAllowedAttributes($object, $context, true);

if (false !== $allowedAttributes) {
if ($context['cache_key']) {
$this->attributesCache[$key] = $allowedAttributes;
}

return $allowedAttributes;
}

$attributes = $this->extractAttributes($object, $format, $context);

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

$allowedAttributes = $this->getAllowedAttributes($object, $context, true);

if (false !== $allowedAttributes) {
$attributes = array_intersect($attributes, $allowedAttributes);
}

if ($context['cache_key'] && \stdClass::class !== $class) {
$this->attributesCache[$key] = $attributes;
}
Expand Down
Expand Up @@ -15,6 +15,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
Expand Down Expand Up @@ -444,6 +445,14 @@ public function testNormalizeEmptyObject()
$normalizedData = $normalizer->normalize(new EmptyDummy(), 'any', ['preserve_empty_objects' => true]);
$this->assertEquals(new \ArrayObject(), $normalizedData);
}

public function testNormalizeWithIgnoreAnnotationAndPrivateProperties()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$serializer = new Serializer([new ObjectNormalizer($classMetadataFactory)]);

$this->assertSame(['foo' => 'foo'], $serializer->normalize(new ObjectDummyWithIgnoreAnnotationAndPrivateProperty()));
}
}

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down Expand Up @@ -484,6 +493,16 @@ class EmptyDummy
{
}

class ObjectDummyWithIgnoreAnnotationAndPrivateProperty
{
public $foo = 'foo';

/** @Ignore */
public $ignored = 'ignored';

private $private = 'private';
}

class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
{
public function __construct()
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Tests/SerializerTest.php
Expand Up @@ -471,7 +471,7 @@ public function testDeserializeAndSerializeInterfacedObjectsWithTheClassMetadata
'groups' => ['two'],
]);

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

public function testDeserializeAndSerializeNestedInterfacedObjectsWithTheClassMetadataDiscriminator()
Expand Down

0 comments on commit 1da3a5c

Please sign in to comment.