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

Skip to content

Commit fc84692

Browse files
committed
[Serializer] Harden the ObjectNormalizer
1 parent db16d52 commit fc84692

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Serializer\Exception\CircularReferenceException;
1515
use Symfony\Component\Serializer\Exception\LogicException;
16+
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
1617

1718
/**
1819
* Base class for a normalizer dealing with objects.
@@ -172,7 +173,11 @@ public function denormalize($data, $class, $format = null, array $context = arra
172173
$ignored = in_array($attribute, $this->ignoredAttributes);
173174

174175
if ($allowed && !$ignored) {
175-
$this->setAttributeValue($object, $attribute, $value, $format, $context);
176+
try {
177+
$this->setAttributeValue($object, $attribute, $value, $format, $context);
178+
} catch (\TypeError $e) {
179+
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
180+
}
176181
}
177182
}
178183

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Normalizer;
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
15+
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
1516
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1617
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1718
use Symfony\Component\Serializer\Serializer;
@@ -498,6 +499,14 @@ public function testMaxDepth()
498499

499500
$this->assertEquals($expected, $result);
500501
}
502+
503+
/**
504+
* @expectedException UnexpectedValueException
505+
*/
506+
public function testThrowUnexpectedValueException()
507+
{
508+
$this->normalizer->denormalize(array('foo' => 'bar'), ObjectTypeHinted::class);
509+
}
501510
}
502511

503512
class ObjectDummy
@@ -658,3 +667,10 @@ public static function getBaz()
658667
return 'L';
659668
}
660669
}
670+
671+
class ObjectTypeHinted
672+
{
673+
public function setFoo(array $f)
674+
{
675+
}
676+
}

src/Symfony/Component/Serializer/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"require-dev": {
2222
"symfony/yaml": "~2.8|~3.0",
2323
"symfony/config": "~2.8|~3.0",
24-
"symfony/property-access": "~2.8|~3.0",
24+
"symfony/property-access": "~3.1",
2525
"symfony/http-foundation": "~2.8|~3.0",
2626
"symfony/cache": "~3.1",
2727
"doctrine/annotations": "~1.0",

0 commit comments

Comments
 (0)