minor #13933 [Serializer] Fix object normalization exceptions (dunglas)
This PR was merged into the 2.6 branch.
Discussion
----------
[Serializer] Fix object normalization exceptions
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #13909
| License | MIT
| Doc PR | n/a
Commits
-------
500df47
[Serializer] Fix object normalization exceptions
This commit is contained in:
commit
051c3de95a
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user