[FrameworkBundle] Register the ArrayDenormalizer

This commit is contained in:
Kévin Dunglas 2016-11-10 11:06:30 +01:00 committed by Fabien Potencier
parent a524ba5e77
commit 2eedafc231
4 changed files with 86 additions and 1 deletions

View File

@ -23,10 +23,15 @@
<argument type="service" id="serializer.property_accessor" />
<argument type="service" id="property_info" on-invalid="ignore" />
<!-- Run after all custom serializers -->
<!-- Run after all custom normalizers -->
<tag name="serializer.normalizer" priority="-1000" />
</service>
<service id="serializer.denormalizer.array" class="Symfony\Component\Serializer\Normalizer\ArrayDenormalizer" public="false">
<!-- Run before the object normalizer -->
<tag name="serializer.normalizer" priority="-990" />
</service>
<!-- Loader -->
<service id="serializer.mapping.chain_loader" class="Symfony\Component\Serializer\Mapping\Loader\LoaderChain" public="false">
<argument type="collection" />

View File

@ -0,0 +1,58 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class SerializerTest extends WebTestCase
{
public function testDeserializeArrayOfObject()
{
if (!class_exists(DataUriNormalizer::class)) {
$this->markTestSkipped('This test is only applicable when using the Symfony Serializer Component version 3.1 or superior.');
}
static::bootKernel(array('test_case' => 'Serializer'));
$container = static::$kernel->getContainer();
$result = $container->get('serializer')->deserialize('{"bars": [{"id": 1}, {"id": 2}]}', Foo::class, 'json');
$bar1 = new Bar();
$bar1->id = 1;
$bar2 = new Bar();
$bar2->id = 2;
$expected = new Foo();
$expected->bars = array($bar1, $bar2);
$this->assertEquals($expected, $result);
}
}
class Foo
{
/**
* @var Bar[]
*/
public $bars;
}
class Bar
{
/**
* @var int
*/
public $id;
}

View File

@ -0,0 +1,16 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
return array(
new FrameworkBundle(),
);

View File

@ -0,0 +1,6 @@
imports:
- { resource: ../config/default.yml }
framework:
serializer: { enabled: true }
property_info: { enabled: true }