Skip to content

Commit a92aa5e

Browse files
gquemenerfabpot
authored andcommitted
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 4986f6d commit a92aa5e

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ public function normalize($object, $format = null, array $context = array())
9999
$attributeValue = call_user_func($this->callbacks[$attributeName], $attributeValue);
100100
}
101101
if (null !== $attributeValue && !is_scalar($attributeValue)) {
102+
if (!$this->serializer instanceof NormalizerInterface) {
103+
throw new \LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attributeName));
104+
}
102105
$attributeValue = $this->serializer->normalize($attributeValue, $format);
103106
}
104107

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,42 @@
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
{
2629
$obj = new GetSetDummy();
30+
$object = new \stdClass();
2731
$obj->setFoo('foo');
2832
$obj->setBar('bar');
2933
$obj->setCamelCase('camelcase');
34+
$obj->setObject($object);
35+
36+
$this->serializer
37+
->expects($this->once())
38+
->method('normalize')
39+
->with($object, 'any')
40+
->will($this->returnValue('string_object'))
41+
;
42+
3043
$this->assertEquals(
31-
array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar', 'camelCase' => 'camelcase'),
44+
array(
45+
'foo' => 'foo',
46+
'bar' => 'bar',
47+
'fooBar' => 'foobar',
48+
'camelCase' => 'camelcase',
49+
'object' => 'string_object',
50+
),
3251
$this->normalizer->normalize($obj, 'any')
3352
);
3453
}
@@ -116,7 +135,7 @@ public function testUncallableCallbacks()
116135

117136
public function testIgnoredAttributes()
118137
{
119-
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase'));
138+
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase', 'object'));
120139

121140
$obj = new GetSetDummy();
122141
$obj->setFoo('foo');
@@ -188,13 +207,30 @@ public function provideCallbacks()
188207
),
189208
);
190209
}
210+
211+
/**
212+
* @expectedException \LogicException
213+
* @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer
214+
*/
215+
public function testUnableToNormalizeObjectAttribute()
216+
{
217+
$serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
218+
$this->normalizer->setSerializer($serializer);
219+
220+
$obj = new GetSetDummy();
221+
$object = new \stdClass();
222+
$obj->setObject($object);
223+
224+
$this->normalizer->normalize($obj, 'any');
225+
}
191226
}
192227

193228
class GetSetDummy
194229
{
195230
protected $foo;
196231
private $bar;
197232
protected $camelCase;
233+
protected $object;
198234

199235
public function getFoo()
200236
{
@@ -235,6 +271,16 @@ public function otherMethod()
235271
{
236272
throw new \RuntimeException("Dummy::otherMethod() should not be called");
237273
}
274+
275+
public function setObject($object)
276+
{
277+
$this->object = $object;
278+
}
279+
280+
public function getObject()
281+
{
282+
return $this->object;
283+
}
238284
}
239285

240286
class GetConstructorDummy
@@ -263,3 +309,7 @@ public function otherMethod()
263309
throw new \RuntimeException("Dummy::otherMethod() should not be called");
264310
}
265311
}
312+
313+
abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface
314+
{
315+
}

0 commit comments

Comments
 (0)