Skip to content

Commit

Permalink
[Serializer] Revert allowed attributes fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed Nov 29, 2023
1 parent 9a1efdb commit c31ec3e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
16 changes: 10 additions & 6 deletions Normalizer/AbstractObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 0 additions & 19 deletions Tests/Normalizer/AbstractObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c31ec3e

Please sign in to comment.