Skip to content

Commit 8a54920

Browse files
committed
[Serializer] 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.
1 parent fc0a09a commit 8a54920

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public function normalize($object, $format = null)
8585
$attributeValue = call_user_func($this->callbacks[$attributeName], $attributeValue);
8686
}
8787
if (null !== $attributeValue && !is_scalar($attributeValue)) {
88+
if (!$this->serializer instanceof NormalizerInterface) {
89+
throw new \LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attributeName));
90+
}
8891
$attributeValue = $this->serializer->normalize($attributeValue, $format);
8992
}
9093

src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,39 @@
1212
namespace Symfony\Component\Serializer\Tests\Normalizer;
1313

1414
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
15+
use Symfony\Component\Serializer\SerializerInterface;
16+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1517

1618
class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
1719
{
1820
protected function setUp()
1921
{
22+
$this->serializer = $this->getMock(__NAMESPACE__.'\SerializerNormalizer');
2023
$this->normalizer = new GetSetMethodNormalizer;
21-
$this->normalizer->setSerializer($this->getMock('Symfony\Component\Serializer\Serializer'));
24+
$this->normalizer->setSerializer($this->serializer);
2225
}
2326

2427
public function testNormalize()
2528
{
26-
$obj = new GetSetDummy;
29+
$obj = new GetSetDummy();
30+
$object = new \stdClass();
2731
$obj->setFoo('foo');
2832
$obj->setBar('bar');
33+
$obj->setObject($object);
34+
35+
$this->serializer
36+
->expects($this->once())
37+
->method('normalize')
38+
->with($object, 'any')
39+
->will($this->returnValue('string_object'));
40+
2941
$this->assertEquals(
30-
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar'),
42+
array(
43+
'foo' => 'foo',
44+
'bar' => 'bar',
45+
'fooBar' => 'foobar',
46+
'object' => 'string_object'
47+
),
3148
$this->normalizer->normalize($obj, 'any')
3249
);
3350
}
@@ -82,7 +99,7 @@ public function testUncallableCallbacks()
8299

83100
public function testIgnoredAttributes()
84101
{
85-
$this->normalizer->setIgnoredAttributes(array('foo', 'bar'));
102+
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'object'));
86103

87104
$obj = new GetSetDummy;
88105
$obj->setFoo('foo');
@@ -154,12 +171,29 @@ public function provideCallbacks()
154171
),
155172
);
156173
}
174+
175+
/**
176+
* @expectedException \LogicException
177+
* @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer
178+
*/
179+
public function testUnableToNormalizeObjectAttribute()
180+
{
181+
$serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
182+
$this->normalizer->setSerializer($serializer);
183+
184+
$obj = new GetSetDummy();
185+
$object = new \stdClass();
186+
$obj->setObject($object);
187+
188+
$this->normalizer->normalize($obj, 'any');
189+
}
157190
}
158191

159192
class GetSetDummy
160193
{
161194
protected $foo;
162195
private $bar;
196+
protected $object;
163197

164198
public function getFoo()
165199
{
@@ -190,6 +224,16 @@ public function otherMethod()
190224
{
191225
throw new \RuntimeException("Dummy::otherMethod() should not be called");
192226
}
227+
228+
public function setObject($object)
229+
{
230+
$this->object = $object;
231+
}
232+
233+
public function getObject()
234+
{
235+
return $this->object;
236+
}
193237
}
194238

195239
class GetConstructorDummy
@@ -218,3 +262,7 @@ public function otherMethod()
218262
throw new \RuntimeException("Dummy::otherMethod() should not be called");
219263
}
220264
}
265+
266+
abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface
267+
{
268+
}

0 commit comments

Comments
 (0)