Throw exception when unable to normalize embedded object
Added a check to ensure that injected serializer of the GetSetMethodNormalizer is a normalizer before normalizing object value.
This commit is contained in:
parent
4986f6db46
commit
a92aa5e849
@ -99,6 +99,9 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal
|
||||
$attributeValue = call_user_func($this->callbacks[$attributeName], $attributeValue);
|
||||
}
|
||||
if (null !== $attributeValue && !is_scalar($attributeValue)) {
|
||||
if (!$this->serializer instanceof NormalizerInterface) {
|
||||
throw new \LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attributeName));
|
||||
}
|
||||
$attributeValue = $this->serializer->normalize($attributeValue, $format);
|
||||
}
|
||||
|
||||
|
@ -12,23 +12,42 @@
|
||||
namespace Symfony\Component\Serializer\Tests\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
$this->serializer = $this->getMock(__NAMESPACE__.'\SerializerNormalizer');
|
||||
$this->normalizer = new GetSetMethodNormalizer();
|
||||
$this->normalizer->setSerializer($this->getMock('Symfony\Component\Serializer\Serializer'));
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
}
|
||||
|
||||
public function testNormalize()
|
||||
{
|
||||
$obj = new GetSetDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setCamelCase('camelcase');
|
||||
$obj->setObject($object);
|
||||
|
||||
$this->serializer
|
||||
->expects($this->once())
|
||||
->method('normalize')
|
||||
->with($object, 'any')
|
||||
->will($this->returnValue('string_object'))
|
||||
;
|
||||
|
||||
$this->assertEquals(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar', 'camelCase' => 'camelcase'),
|
||||
array(
|
||||
'foo' => 'foo',
|
||||
'bar' => 'bar',
|
||||
'fooBar' => 'foobar',
|
||||
'camelCase' => 'camelcase',
|
||||
'object' => 'string_object',
|
||||
),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
@ -116,7 +135,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testIgnoredAttributes()
|
||||
{
|
||||
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase'));
|
||||
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase', 'object'));
|
||||
|
||||
$obj = new GetSetDummy();
|
||||
$obj->setFoo('foo');
|
||||
@ -188,6 +207,22 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer
|
||||
*/
|
||||
public function testUnableToNormalizeObjectAttribute()
|
||||
{
|
||||
$serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$obj = new GetSetDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setObject($object);
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
}
|
||||
|
||||
class GetSetDummy
|
||||
@ -195,6 +230,7 @@ class GetSetDummy
|
||||
protected $foo;
|
||||
private $bar;
|
||||
protected $camelCase;
|
||||
protected $object;
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
@ -235,6 +271,16 @@ class GetSetDummy
|
||||
{
|
||||
throw new \RuntimeException("Dummy::otherMethod() should not be called");
|
||||
}
|
||||
|
||||
public function setObject($object)
|
||||
{
|
||||
$this->object = $object;
|
||||
}
|
||||
|
||||
public function getObject()
|
||||
{
|
||||
return $this->object;
|
||||
}
|
||||
}
|
||||
|
||||
class GetConstructorDummy
|
||||
@ -263,3 +309,7 @@ class GetConstructorDummy
|
||||
throw new \RuntimeException("Dummy::otherMethod() should not be called");
|
||||
}
|
||||
}
|
||||
|
||||
abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface
|
||||
{
|
||||
}
|
||||
|
Reference in New Issue
Block a user