[Serializer] Harden the ObjectNormalizer
This commit is contained in:
parent
fa01e848d0
commit
26a07fb2ab
@ -11,8 +11,10 @@
|
||||
|
||||
namespace Symfony\Component\Serializer\Normalizer;
|
||||
|
||||
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Serializer\Exception\CircularReferenceException;
|
||||
use Symfony\Component\Serializer\Exception\LogicException;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Base class for a normalizer dealing with objects.
|
||||
@ -172,7 +174,11 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
|
||||
$ignored = in_array($attribute, $this->ignoredAttributes);
|
||||
|
||||
if ($allowed && !$ignored) {
|
||||
$this->setAttributeValue($object, $attribute, $value, $format, $context);
|
||||
try {
|
||||
$this->setAttributeValue($object, $attribute, $value, $format, $context);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -498,6 +498,14 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testThrowUnexpectedValueException()
|
||||
{
|
||||
$this->normalizer->denormalize(array('foo' => 'bar'), ObjectTypeHinted::class);
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectDummy
|
||||
@ -658,3 +666,10 @@ class ObjectWithStaticPropertiesAndMethods
|
||||
return 'L';
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectTypeHinted
|
||||
{
|
||||
public function setFoo(array $f)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,9 @@
|
||||
"doctrine/annotations": "~1.0",
|
||||
"doctrine/cache": "~1.0"
|
||||
},
|
||||
"conflict": {
|
||||
"symfony/property-access": ">=3.0,<3.0.4|>=2.8,<2.8.4"
|
||||
},
|
||||
"suggest": {
|
||||
"psr/cache-implementation": "For using the metadata cache.",
|
||||
"symfony/yaml": "For using the default YAML mapping loader.",
|
||||
|
Reference in New Issue
Block a user