From c31ec3e7ffb6c8681c732bcc0bcbd8b84ce8d90f Mon Sep 17 00:00:00 2001 From: Mathias Arlaud Date: Wed, 29 Nov 2023 10:47:40 +0100 Subject: [PATCH] [Serializer] Revert allowed attributes fix --- Normalizer/AbstractObjectNormalizer.php | 16 ++++++++++------ .../AbstractObjectNormalizerTest.php | 19 ------------------- Tests/SerializerTest.php | 2 +- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/Normalizer/AbstractObjectNormalizer.php b/Normalizer/AbstractObjectNormalizer.php index b7bf2e15d..732586bd8 100644 --- a/Normalizer/AbstractObjectNormalizer.php +++ b/Normalizer/AbstractObjectNormalizer.php @@ -309,16 +309,20 @@ protected function getAttributes(object $object, ?string $format, array $context return $this->attributesCache[$key]; } - $attributes = $this->extractAttributes($object, $format, $context); + $allowedAttributes = $this->getAllowedAttributes($object, $context, true); - if ($this->classDiscriminatorResolver && $mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object)) { - array_unshift($attributes, $mapping->getTypeProperty()); + if (false !== $allowedAttributes) { + if ($context['cache_key']) { + $this->attributesCache[$key] = $allowedAttributes; + } + + return $allowedAttributes; } - $allowedAttributes = $this->getAllowedAttributes($object, $context, true); + $attributes = $this->extractAttributes($object, $format, $context); - if (false !== $allowedAttributes) { - $attributes = $attributes ? array_intersect($attributes, $allowedAttributes) : $allowedAttributes; + if ($this->classDiscriminatorResolver && $mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object)) { + array_unshift($attributes, $mapping->getTypeProperty()); } if ($context['cache_key'] && \stdClass::class !== $class) { diff --git a/Tests/Normalizer/AbstractObjectNormalizerTest.php b/Tests/Normalizer/AbstractObjectNormalizerTest.php index cf33e8e60..35616b658 100644 --- a/Tests/Normalizer/AbstractObjectNormalizerTest.php +++ b/Tests/Normalizer/AbstractObjectNormalizerTest.php @@ -17,7 +17,6 @@ use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; use Symfony\Component\PropertyInfo\PropertyInfoExtractor; 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; @@ -455,14 +454,6 @@ public function testNormalizeEmptyObject() $this->assertEquals(new \ArrayObject(), $normalizedData); } - public function testNormalizeWithIgnoreAnnotationAndPrivateProperties() - { - $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $normalizer = new ObjectNormalizer($classMetadataFactory); - - $this->assertSame(['foo' => 'foo'], $normalizer->normalize(new ObjectDummyWithIgnoreAnnotationAndPrivateProperty())); - } - public function testNormalizeBasedOnAllowedAttributes() { $normalizer = new class() extends AbstractObjectNormalizer { @@ -586,16 +577,6 @@ class EmptyDummy { } -class ObjectDummyWithIgnoreAnnotationAndPrivateProperty -{ - public $foo = 'foo'; - - /** @Ignore */ - public $ignored = 'ignored'; - - private $private = 'private'; -} - class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer { public function __construct() diff --git a/Tests/SerializerTest.php b/Tests/SerializerTest.php index 65f7fd9d5..12d8eeefd 100644 --- a/Tests/SerializerTest.php +++ b/Tests/SerializerTest.php @@ -473,7 +473,7 @@ public function testDeserializeAndSerializeInterfacedObjectsWithTheClassMetadata 'groups' => ['two'], ]); - $this->assertEquals('{"type":"one","two":2}', $serialized); + $this->assertEquals('{"two":2,"type":"one"}', $serialized); } public function testDeserializeAndSerializeNestedInterfacedObjectsWithTheClassMetadataDiscriminator()