Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit313fca9

Browse files
committed
throw exception when extra attributes are used during an object denormalization
1 parentfba0638 commit313fca9

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 <fabien@symfony.com>
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+
namespaceSymfony\Component\Serializer\Exception;
13+
14+
/**
15+
* ExtraAttributesException.
16+
*
17+
* @author Julien DIDIER <julien@didier.io>
18+
*/
19+
class ExtraAttributesExceptionextends RuntimeException
20+
{
21+
publicfunction__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
useSymfony\Component\PropertyAccess\Exception\InvalidArgumentException;
1515
useSymfony\Component\Serializer\Exception\CircularReferenceException;
16+
useSymfony\Component\Serializer\Exception\ExtraAttributesException;
1617
useSymfony\Component\Serializer\Exception\LogicException;
1718
useSymfony\Component\Serializer\Exception\UnexpectedValueException;
1819
useSymfony\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+
thrownewExtraAttributesException($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 =newAbstractObjectNormalizerDummy();
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+
publicfunctiontestDenormalizeWithExtraAttributes()
46+
{
47+
$normalizer =newAbstractObjectNormalizerDummy();
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 AbstractObjectNormalizerDummyextends AbstractObjectNormalizer

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp