Skip to content

Commit 31f3eb9

Browse files
committed
[Serializer] Fix object normalization exceptions
1 parent 72e6c69 commit 31f3eb9

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

Normalizer/GetSetMethodNormalizer.php

Lines changed: 2 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\InvalidArgumentException;
16+
use Symfony\Component\Serializer\Exception\LogicException;
1617
use Symfony\Component\Serializer\Exception\RuntimeException;
1718

1819
/**
@@ -168,7 +169,7 @@ public function normalize($object, $format = null, array $context = array())
168169
}
169170
if (null !== $attributeValue && !is_scalar($attributeValue)) {
170171
if (!$this->serializer instanceof NormalizerInterface) {
171-
throw new \LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attributeName));
172+
throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attributeName));
172173
}
173174

174175
$attributeValue = $this->serializer->normalize($attributeValue, $format, $context);

Normalizer/PropertyNormalizer.php

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

1414
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15+
use Symfony\Component\Serializer\Exception\LogicException;
1516
use Symfony\Component\Serializer\Exception\RuntimeException;
1617

1718
/**
@@ -100,6 +101,10 @@ public function normalize($object, $format = null, array $context = array())
100101
$attributeValue = call_user_func($this->callbacks[$property->name], $attributeValue);
101102
}
102103
if (null !== $attributeValue && !is_scalar($attributeValue)) {
104+
if (!$this->serializer instanceof NormalizerInterface) {
105+
throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $property->name));
106+
}
107+
103108
$attributeValue = $this->serializer->normalize($attributeValue, $format);
104109
}
105110

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public function provideCallbacks()
260260
}
261261

262262
/**
263-
* @expectedException \LogicException
263+
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
264264
* @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer
265265
*/
266266
public function testUnableToNormalizeObjectAttribute()

Tests/Normalizer/PropertyNormalizerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@ public function provideCallbacks()
195195
),
196196
);
197197
}
198+
199+
/**
200+
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
201+
* @expectedExceptionMessage Cannot normalize attribute "bar" because injected serializer is not a normalizer
202+
*/
203+
public function testUnableToNormalizeObjectAttribute()
204+
{
205+
$serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
206+
$this->normalizer->setSerializer($serializer);
207+
208+
$obj = new PropertyDummy();
209+
$object = new \stdClass();
210+
$obj->setBar($object);
211+
212+
$this->normalizer->normalize($obj, 'any');
213+
}
198214
}
199215

200216
class PropertyDummy

0 commit comments

Comments
 (0)