Skip to content

Commit

Permalink
[Serializer] Fix access to private when Ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed Nov 22, 2023
1 parent 48be4b3 commit f8c767d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 testNormalizeWithIgnoreAttributeAndPrivateProperties()
{
$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 string $foo = 'foo';

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

private string $private = 'private';
}

class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
{
public function __construct()
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
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 f8c767d

Please sign in to comment.