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

Skip to content

Commit 5d4d0a6

Browse files
committed
[Serializer] Harden the ObjectNormalizer
1 parent fa01e84 commit 5d4d0a6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-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+
}

0 commit comments

Comments
 (0)