Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #20480 [FrameworkBundle] Register the ArrayDenormalizer (dunglas)
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
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SerializerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Serializer/bundles.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
); |
6 changes: 6 additions & 0 deletions
6
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Serializer/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
imports: | ||
- { resource: ../config/default.yml } | ||
|
||
framework: | ||
serializer: { enabled: true } | ||
property_info: { enabled: true } |