8000 allow_extra_attributes does not throw an exception as documented · symfony/symfony@a67b650 · GitHub
[go: up one dir, main page]

Skip to content

Commit a67b650

Browse files
deviantintegralogizanagi
authored andcommitted
allow_extra_attributes does not throw an exception as documented
1 parent 15b9b07 commit a67b650

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
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
206208
{
207209
if (!$this->classMetadataFactory) {
210+
if (isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) && !$context[static::ALLOW_EXTRA_ATTRIBUTES]) {
211+
throw new LogicException(sprintf("A class metadata factory must be provided in the constructor when setting '%s' to false.", static::ALLOW_EXTRA_ATTRIBUTES));
212+
}
213+
208214
return false;
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
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
2020
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
2121
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
22+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
2223
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2324
use Symfony\Component\Serializer\SerializerAwareInterface;
2425
use Symfony\Component\Serializer\SerializerInterface;
@@ -52,7 +53,8 @@ public function testInstantiateObjectDenormalizer()
5253
*/
5354
public function testDenormalizeWithExtraAttributes()
5455
{
55-
$normalizer = new AbstractObjectNormalizerDummy();
56+
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
57+
$normalizer = new AbstractObjectNormalizerDummy($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+
public function testExtraAttributesException()
159+
{
160+
$normalizer = new ObjectNormalizer();
161+
162+
$normalizer->denormalize(array(), \stdClass::class, 'xml', array(
163+
'allow_extra_attributes' => false,
164+
));
165+
}
147166
}
148167

149168
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer

0 commit comments

Comments
 (0)
0