[Serializer] Fix object normalization exceptions

This commit is contained in:
Kévin Dunglas 2015-03-16 00:12:13 +01:00
parent be053446bd
commit 500df475a6
4 changed files with 24 additions and 2 deletions

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Serializer\Normalizer;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\RuntimeException;
/**
@ -168,7 +169,7 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal
}
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));
throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attributeName));
}
$attributeValue = $this->serializer->normalize($attributeValue, $format, $context);

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Normalizer;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\RuntimeException;
/**
@ -100,6 +101,10 @@ class PropertyNormalizer extends SerializerAwareNormalizer implements Normalizer
$attributeValue = call_user_func($this->callbacks[$property->name], $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', $property->name));
}
$attributeValue = $this->serializer->normalize($attributeValue, $format);
}

View File

@ -260,7 +260,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException \LogicException
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
* @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer
*/
public function testUnableToNormalizeObjectAttribute()

View File

@ -195,6 +195,22 @@ class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
),
);
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
* @expectedExceptionMessage Cannot normalize attribute "bar" because injected serializer is not a normalizer
*/
public function testUnableToNormalizeObjectAttribute()
{
$serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
$this->normalizer->setSerializer($serializer);
$obj = new PropertyDummy();
$object = new \stdClass();
$obj->setBar($object);
$this->normalizer->normalize($obj, 'any');
}
}
class PropertyDummy