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

Commita67b650

Browse files
deviantintegralogizanagi
authored andcommitted
allow_extra_attributes does not throw an exception as documented
1 parent15b9b07 commita67b650

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,17 @@ protected function handleCircularReference($object)
200200
* @param array $context
201201
* @param bool $attributesAsString If false, return an array of {@link AttributeMetadataInterface}
202202
*
203+
* @throws \Symfony\Component\Serializer\Exception\LogicException if the 'allow_extra_attributes' context variable is false and no class metadata factory is provided
204+
*
203205
* @return string[]|AttributeMetadataInterface[]|bool
204206
*/
205207
protectedfunctiongetAllowedAttributes($classOrObject,array$context,$attributesAsString =false)
206208
{
207209
if (!$this->classMetadataFactory) {
210+
if (isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) && !$context[static::ALLOW_EXTRA_ATTRIBUTES]) {
211+
thrownewLogicException(sprintf("A class metadata factory must be provided in the constructor when setting '%s' to false.",static::ALLOW_EXTRA_ATTRIBUTES));
212+
}
213+
208214
returnfalse;
209215
}
210216

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
useSymfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
2020
useSymfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
2121
useSymfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
22+
useSymfony\Component\Serializer\Normalizer\ObjectNormalizer;
2223
useSymfony\Component\Serializer\Normalizer\DenormalizerInterface;
2324
useSymfony\Component\Serializer\SerializerAwareInterface;
2425
useSymfony\Component\Serializer\SerializerInterface;
@@ -52,7 +53,8 @@ public function testInstantiateObjectDenormalizer()
5253
*/
5354
publicfunctiontestDenormalizeWithExtraAttributes()
5455
{
55-
$normalizer =newAbstractObjectNormalizerDummy();
56+
$factory =newClassMetadataFactory(newAnnotationLoader(newAnnotationReader()));
57+
$normalizer =newAbstractObjectNormalizerDummy($factory);
5658
$normalizer->denormalize(
5759
array('fooFoo' =>'foo','fooBar' =>'bar'),
5860
__NAMESPACE__.'\Dummy',
@@ -144,6 +146,23 @@ private function getDenormalizerForDummyCollection()
144146

145147
return$denormalizer;
146148
}
149+
150+
/**
151+
* Test that additional attributes throw an exception if no metadata factory is specified.
152+
*
153+
* @see https://symfony.com/doc/current/components/serializer.html#deserializing-an-object
154+
*
155+
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
156+
* @expectedExceptionMessage A class metadata factory must be provided in the constructor when setting 'allow_extra_attributes' to false.
157+
*/
158+
publicfunctiontestExtraAttributesException()
159+
{
160+
$normalizer =newObjectNormalizer();
161+
162+
$normalizer->denormalize(array(), \stdClass::class,'xml',array(
163+
'allow_extra_attributes' =>false,
164+
));
165+
}
147166
}
148167

149168
class AbstractObjectNormalizerDummyextends AbstractObjectNormalizer

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp