Skip to content

Commit

Permalink
bug #20480 [FrameworkBundle] Register the ArrayDenormalizer (dunglas)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.1 branch (closes #20480).

Discussion
----------

[FrameworkBundle] Register the ArrayDenormalizer

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Registers the `ArrayDenormalizer` class in FrameworkBundle.

**Why it's a bug fix?**

Because since 3.1, most normalizers are able to deserialize complex types (e.g.: an object embedded in an object). They use the `Class\Name[]` notation to handle collections.

However, this only works when the `ArrayDenormalizer` has been registered (it is responsible of handling the `[]` notation).
We do it manually in unit tests, but `ArrayDenormalizer` has never been integrated in FrameworkBundle.

See the test case for further details.

Commits
-------

2eedafc [FrameworkBundle] Register the ArrayDenormalizer
  • Loading branch information
fabpot committed Nov 11, 2016
2 parents 75e0046 + 2eedafc commit c4989c5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
Expand Up @@ -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" />
Expand Down
@@ -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;
}
@@ -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(),
);
@@ -0,0 +1,6 @@
imports:
- { resource: ../config/default.yml }

framework:
serializer: { enabled: true }
property_info: { enabled: true }

0 comments on commit c4989c5

Please sign in to comment.