Skip to content

Commit c57cb8e

Browse files
committed
bug symfony#24816 [Serializer] Fix extra attributes when no group specified (ogizanagi)
This PR was merged into the 3.3 branch. Discussion ---------- [Serializer] Fix extra attributes when no group specified | Q | A | ------------- | --- | Branch? | 3.3 <!-- see comment below --> | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | symfony#24783 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A ~~Two commits, for two possible solutions, but I think the last one is probably the most efficient one, as the first one will also impact normalization and systematically try to intersect allowedAttributes and extractedAttributes.~~ Commits ------- d1b343c [Serializer] Fix extra attributes when no group specified
2 parents 0c2d77c + d1b343c commit c57cb8e

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
3131
const OBJECT_TO_POPULATE = 'object_to_populate';
3232
const GROUPS = 'groups';
3333
const ATTRIBUTES = 'attributes';
34+
const ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes';
3435

3536
/**
3637
* @var int
@@ -201,7 +202,14 @@ protected function handleCircularReference($object)
201202
*/
202203
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
203204
{
204-
if (!$this->classMetadataFactory || !isset($context[static::GROUPS]) || !is_array($context[static::GROUPS])) {
205+
if (!$this->classMetadataFactory) {
206+
return false;
207+
}
208+
209+
$groups = false;
210+
if (isset($context[static::GROUPS]) && is_array($context[static::GROUPS])) {
211+
$groups = $context[static::GROUPS];
212+
} elseif (!isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) || $context[static::ALLOW_EXTRA_ATTRIBUTES]) {
205213
return false;
206214
}
207215

@@ -210,7 +218,7 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
210218
$name = $attributeMetadata->getName();
211219

212220
if (
213-
count(array_intersect($attributeMetadata->getGroups(), $context[static::GROUPS])) &&
221+
(false === $groups || count(array_intersect($attributeMetadata->getGroups(), $groups))) &&
214222
$this->isAllowedAttribute($classOrObject, $name, null, $context)
215223
) {
216224
$allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
3131
{
3232
const ENABLE_MAX_DEPTH = 'enable_max_depth';
3333
const DEPTH_KEY_PATTERN = 'depth_%s::%s';
34-
const ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes';
3534

3635
private $propertyTypeExtractor;
3736
private $attributesCache = array();

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
namespace Symfony\Component\Serializer\Tests\Normalizer;
1313

14+
use Doctrine\Common\Annotations\AnnotationReader;
1415
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
17+
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
1518
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
1619

1720
class AbstractObjectNormalizerTest extends TestCase
@@ -51,6 +54,21 @@ public function testDenormalizeWithExtraAttributes()
5154
array('allow_extra_attributes' => false)
5255
);
5356
}
57+
58+
/**
59+
* @expectedException \Symfony\Component\Serializer\Exception\ExtraAttributesException
60+
* @expectedExceptionMessage Extra attributes are not allowed ("fooFoo", "fooBar" are unknown).
61+
*/
62+
public function testDenormalizeWithExtraAttributesAndNoGroupsWithMetadataFactory()
63+
{
64+
$normalizer = new AbstractObjectNormalizerWithMetadata();
65+
$normalizer->denormalize(
66+
array('fooFoo' => 'foo', 'fooBar' => 'bar', 'bar' => 'bar'),
67+
Dummy::class,
68+
'any',
69+
array('allow_extra_attributes' => false)
70+
);
71+
}
5472
}
5573

5674
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -85,3 +103,24 @@ class Dummy
85103
public $bar;
86104
public $baz;
87105
}
106+
107+
class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
108+
{
109+
public function __construct()
110+
{
111+
parent::__construct(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
112+
}
113+
114+
protected function extractAttributes($object, $format = null, array $context = array())
115+
{
116+
}
117+
118+
protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
119+
{
120+
}
121+
122+
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
123+
{
124+
$object->$attribute = $value;
125+
}
126+
}

0 commit comments

Comments
 (0)