Skip to content

Commit 26a07fb

Browse files
committed
[Serializer] Harden the ObjectNormalizer
1 parent fa01e84 commit 26a07fb

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14+
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
1415
use Symfony\Component\Serializer\Exception\CircularReferenceException;
1516
use Symfony\Component\Serializer\Exception\LogicException;
17+
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
1618

1719
/**
1820
* Base class for a normalizer dealing with objects.
@@ -172,7 +174,11 @@ public function denormalize($data, $class, $format = null, array $context = arra
172174
$ignored = in_array($attribute, $this->ignoredAttributes);
173175

174176
if ($allowed && !$ignored) {
175-
$this->setAttributeValue($object, $attribute, $value, $format, $context);
177+
try {
178+
$this->setAttributeValue($object, $attribute, $value, $format, $context);
179+
} catch (InvalidArgumentException $e) {
180+
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
181+
}
176182
}
177183
}
178184

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,14 @@ public function testMaxDepth()
498498

499499
$this->assertEquals($expected, $result);
500500
}
501+
502+
/**
503+
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
504+
*/
505+
public function testThrowUnexpectedValueException()
506+
{
507+
$this->normalizer->denormalize(array('foo' => 'bar'), ObjectTypeHinted::class);
508+
}
501509
}
502510

503511
class ObjectDummy
@@ -658,3 +666,10 @@ public static function getBaz()
658666
return 'L';
659667
}
660668
}
669+
670+
class ObjectTypeHinted
671+
{
672+
public function setFoo(array $f)
673+
{
674+
}
675+
}

src/Symfony/Component/Serializer/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
"doctrine/annotations": "~1.0",
2828
"doctrine/cache": "~1.0"
2929
},
30+
"conflict": {
31+
"symfony/property-access": ">=3.0,<3.0.4|>=2.8,<2.8.4"
32+
},
3033
"suggest": {
3134
"psr/cache-implementation": "For using the metadata cache.",
3235
"symfony/yaml": "For using the default YAML mapping loader.",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy