Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fb96f68

Browse filesBrowse files
committed
[Serializer] Fix normalization relying on allowed attributes only
1 parent dcbfcb4 commit fb96f68
Copy full SHA for fb96f68

File tree

2 files changed

+33
-3
lines changed
Filter options

2 files changed

+33
-3
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+1-1Lines changed: 1 addition & 1 deletion
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php
+32-2Lines changed: 32 additions & 2 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.