Skip to content

Commit ccead6e

Browse files
bug symfony#52767 [Serializer] Fix normalization relying on allowed attributes only (mtarld)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix normalization relying on allowed attributes only | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#52744 | License | MIT Commits ------- ca16751 [Serializer] Fix normalization relying on allowed attributes only
2 parents dcbfcb4 + ca16751 commit ccead6e

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ protected function getAttributes(object $object, ?string $format, array $context
318318
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
319319

320320
if (false !== $allowedAttributes) {
321-
$attributes = array_intersect($attributes, $allowedAttributes);
321+
$attributes = $attributes ? array_intersect($attributes, $allowedAttributes) : $allowedAttributes;
322322
}
323323

324324
if ($context['cache_key'] && \stdClass::class !== $class) {

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

+32-2
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,39 @@ public function testNormalizeEmptyObject()
458458
public function testNormalizeWithIgnoreAnnotationAndPrivateProperties()
459459
{
460460
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
461-
$serializer = new Serializer([new ObjectNormalizer($classMetadataFactory)]);
461+
$normalizer = new ObjectNormalizer($classMetadataFactory);
462462

463-
$this->assertSame(['foo' => 'foo'], $serializer->normalize(new ObjectDummyWithIgnoreAnnotationAndPrivateProperty()));
463+
$this->assertSame(['foo' => 'foo'], $normalizer->normalize(new ObjectDummyWithIgnoreAnnotationAndPrivateProperty()));
464+
}
465+
466+
public function testNormalizeBasedOnAllowedAttributes()
467+
{
468+
$normalizer = new class() extends AbstractObjectNormalizer {
469+
protected function getAllowedAttributes($classOrObject, array $context, bool $attributesAsString = false)
470+
{
471+
return ['foo'];
472+
}
473+
474+
protected function extractAttributes(object $object, string $format = null, array $context = []): array
475+
{
476+
return [];
477+
}
478+
479+
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
480+
{
481+
return $object->$attribute;
482+
}
483+
484+
protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = [])
485+
{
486+
}
487+
};
488+
489+
$object = new Dummy();
490+
$object->foo = 'foo';
491+
$object->bar = 'bar';
492+
493+
$this->assertSame(['foo' => 'foo'], $normalizer->normalize($object));
464494
}
465495

466496
/**

0 commit comments

Comments
 (0)