Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 24d8135

Browse files
committed
feature #19958 [Serializer] Throw exception when extra attributes are used during an object denor… (juliendidier)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Serializer] Throw exception when extra attributes are used during an object denor… | Q | A | | --- | --- | | Branch? | "master" | | Bug fix? | no | | New feature? | yes | | BC breaks? | no | | Deprecations? | no | | Tests pass? | yes | | Fixed tickets | #19948 | | License | MIT | | Doc PR | [#6975](symfony/symfony-docs#6975) | I will update the doc if you're ok with this PR. Commits ------- 565a984 throw exception when extra attributes are used during an object denormalization
2 parents 60b4dd0 + 565a984 commit 24d8135

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Exception;
13+
14+
/**
15+
* ExtraAttributesException.
16+
*
17+
* @author Julien DIDIER <[email protected]>
18+
*/
19+
class ExtraAttributesException extends RuntimeException
20+
{
21+
public function __construct(array $extraAttributes, \Exception $previous = null)
22+
{
23+
$msg = sprintf('Extra attributes are not allowed ("%s" are unknown).', implode('", "', $extraAttributes));
24+
25+
parent::__construct($msg, 0, $previous);
26+
}
27+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
1515
use Symfony\Component\Serializer\Exception\CircularReferenceException;
16+
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
1617
use Symfony\Component\Serializer\Exception\LogicException;
1718
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
1819
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
@@ -171,8 +172,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
171172
if (!isset($context['cache_key'])) {
172173
$context['cache_key'] = $this->getCacheKey($format, $context);
173174
}
175+
174176
$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
175177
$normalizedData = $this->prepareForDenormalization($data);
178+
$extraAttributes = array();
176179

177180
$reflectionClass = new \ReflectionClass($class);
178181
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
@@ -183,6 +186,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
183186
}
184187

185188
if (($allowedAttributes !== false && !in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
189+
if (isset($context['allow_extra_attributes']) && !$context['allow_extra_attributes']) {
190+
$extraAttributes[] = $attribute;
191+
}
192+
186193
continue;
187194
}
188195

@@ -194,6 +201,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
194201
}
195202
}
196203

204+
if (!empty($extraAttributes)) {
205+
throw new ExtraAttributesException($extraAttributes);
206+
}
207+
197208
return $object;
198209
}
199210

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ public function testInstantiateObjectDenormalizer()
3737
$normalizer = new AbstractObjectNormalizerDummy();
3838
$normalizer->instantiateObject($data, $class, $context, new \ReflectionClass($class), array());
3939
}
40+
41+
/**
42+
* @expectedException \Symfony\Component\Serializer\Exception\ExtraAttributesException
43+
* @expectedExceptionMessage Extra attributes are not allowed ("fooFoo", "fooBar" are unknown).
44+
*/
45+
public function testDenormalizeWithExtraAttributes()
46+
{
47+
$normalizer = new AbstractObjectNormalizerDummy();
48+
$normalizer->denormalize(
49+
array('fooFoo' => 'foo', 'fooBar' => 'bar'),
50+
__NAMESPACE__.'\Dummy',
51+
'any',
52+
array('allow_extra_attributes' => false)
53+
);
54+
}
4055
}
4156

4257
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer

0 commit comments

Comments
 (0)