Skip to content

Commit 49e1faa

Browse files
dunglasfabpot
authored andcommitted
[FrameworkBundle] Register the ArrayDenormalizer
1 parent 24232e8 commit 49e1faa

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

Resources/config/serializer.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@
2323
<argument type="service" id="serializer.property_accessor" />
2424
<argument type="service" id="property_info" on-invalid="ignore" />
2525

26-
<!-- Run after all custom serializers -->
26+
<!-- Run after all custom normalizers -->
2727
<tag name="serializer.normalizer" priority="-1000" />
2828
</service>
2929

30+
<service id="serializer.denormalizer.array" class="Symfony\Component\Serializer\Normalizer\ArrayDenormalizer" public="false">
31+
<!-- Run before the object normalizer -->
32+
<tag name="serializer.normalizer" priority="-990" />
33+
</service>
34+
3035
<!-- Loader -->
3136
<service id="serializer.mapping.chain_loader" class="Symfony\Component\Serializer\Mapping\Loader\LoaderChain" public="false">
3237
<argument type="collection" />

Tests/Functional/SerializerTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
15+
16+
/**
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
class SerializerTest extends WebTestCase
20+
{
21+
public function testDeserializeArrayOfObject()
22+
{
23+
if (!class_exists(DataUriNormalizer::class)) {
24+
$this->markTestSkipped('This test is only applicable when using the Symfony Serializer Component version 3.1 or superior.');
25+
}
26+
27+
static::bootKernel(array('test_case' => 'Serializer'));
28+
$container = static::$kernel->getContainer();
29+
30+
$result = $container->get('serializer')->deserialize('{"bars": [{"id": 1}, {"id": 2}]}', Foo::class, 'json');
31+
32+
$bar1 = new Bar();
33+
$bar1->id = 1;
34+
$bar2 = new Bar();
35+
$bar2->id = 2;
36+
37+
$expected = new Foo();
38+
$expected->bars = array($bar1, $bar2);
39+
40+
$this->assertEquals($expected, $result);
41+
}
42+
}
43+
44+
class Foo
45+
{
46+
/**
47+
* @var Bar[]
48+
*/
49+
public $bars;
50+
}
51+
52+
class Bar
53+
{
54+
/**
55+
* @var int
56+
*/
57+
public $id;
58+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
14+
return array(
15+
new FrameworkBundle(),
16+
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
framework:
5+
serializer: { enabled: true }
6+
property_info: { enabled: true }

0 commit comments

Comments
 (0)