10000 merged branch linclark/7971-error-denormalizeObject (PR #7971) · symfony/symfony@a5ab1aa · GitHub
[go: up one dir, main page]

Skip to content

Commit a5ab1aa

Browse files
committed
merged branch linclark/7971-error-denormalizeObject (PR #7971)
This PR was submitted for the master branch but it was merged into the 2.2 branch instead (closes #7971). Discussion ---------- [Serializer] Fatal error in denormalizeObject for Normalizers that do not implement DenormalizerInterface | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | 7971 | License | MIT | Doc PR | If there is a list of Normalizers, and only some of them implement DenormalizerInterface, then a call to deserialize() can result in a fatal error. This is because denormalizeObject does not check that the Normalizer implements DenormalizerInterface before calling supportsDenormalization on it. Commits ------- cacca2f [Serializer] Fixed fatal error in normalize/denormalizeObject.
2 parents c59c10c + 60edc58 commit a5ab1aa

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ private function normalizeObject($object, $format = null, array $context = array
239239
}
240240

241241
foreach ($this->normalizers as $normalizer) {
242-
if ($normalizer->supportsNormalization($object, $format)) {
242+
if ($normalizer instanceof NormalizerInterface
243+
&& $normalizer->supportsNormalization($object, $format)) {
243244
$this->normalizerCache[$class][$format] = $normalizer;
244245

245246
return $normalizer->normalize($object, $format, $context);
@@ -273,7 +274,8 @@ private function denormalizeObject($data, $class, $format = null, array $context
273274
}
274275

275276
foreach ($this->normalizers as $normalizer) {
276-
if ($normalizer->supportsDenormalization($data, $class, $format)) {
277+
if ($normalizer instanceof DenormalizerInterface
278+
&& $normalizer->supportsDenormalization($data, $class, $format)) {
277279
$this->denormalizerCache[$class][$format] = $normalizer;
278280

279281
return $normalizer->denormalize($data, $class, $format, $context);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Normalizer;
13+
14+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
15+
16+
/**
17+
* Provides a test Normalizer which only implements the DenormalizerInterface.
18+
*
19+
* @author Lin Clark <lin@lin-clark.com>
20+
*/
21+
class TestDenormalizer implements DenormalizerInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function denormalize($data, $class, $format = null, array $context = array())
27+
{
28+
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function supportsDenormalization($data, $type, $format = null)
35+
{
36+
return TRUE;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Normalizer;
13+
14+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
15+
16+
/**
17+
* Provides a test Normalizer which only implements the NormalizerInterface.
18+
*
19+
* @author Lin Clark <lin@lin-clark.com>
20+
*/
21+
class TestNormalizer implements NormalizerInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function normalize($object, $format = null, array $context = array())
27+
{
28+
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function supportsNormalization($data, $format = null)
35+
{
36+
return TRUE;
37+
}
38+
}

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
1818
use Symfony\Co B41A mponent\Serializer\Tests\Fixtures\TraversableDummy;
1919
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
20+
use Symfony\Component\Serializer\Tests\Normalizer\TestNormalizer;
21+
use Symfony\Component\Serializer\Tests\Normalizer\TestDenormalizer;
2022

2123
class SerializerTest extends \PHPUnit_Framework_TestCase
2224
{
@@ -43,6 +45,15 @@ public function testNormalizeGivesPriorityToInterfaceOverTraversable()
4345
$this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
4446
}
4547

48+
/**
49+
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
50+
*/
51+
public function testNormalizeOnDenormalizer()
52+
{
53+
$this->serializer = new Serializer(array(new TestDenormalizer()), array());
54+
$this->assertTrue($this->serializer->normalize(new \stdClass, 'json'));
55+
}
56+
4657
/**
4758
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
4859
*/
@@ -52,6 +63,16 @@ public function testDenormalizeNoMatch()
5263
$this->serializer->denormalize('foo', 'stdClass');
5364
}
5465

66+
/**
67+
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
68+
*/
69+
public function testDenormalizeOnNormalizer()
70+
{
71+
$this->serializer = new 6338 Serializer(array(new TestNormalizer()), array());
72+
$data = array('title' => 'foo', 'numbers' => array(5, 3));
73+
$this->assertTrue($this->serializer->denormalize(json_encode($data), 'stdClass', 'json'));
74+
}
75+
5576
public function testSerialize()
5677
{
5778
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));

0 commit comments

Comments
 (0)
0