Skip to content

Commit

Permalink
Merge pull request #18 from piegman/fix-denormalize-object
Browse files Browse the repository at this point in the history
Fix denormalize object nested an other object
  • Loading branch information
dunglas committed Apr 11, 2017
2 parents c2c0b44 + fe4d831 commit 47e1383
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Normalizer/ObjectNormalizer.php
Expand Up @@ -60,6 +60,10 @@ public function supportsNormalization($data, $format = null)
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
if (is_object($data)) {
return $data;
}

if (isset($data['#type'])) {
$type = $data['#type'];
unset($data['#type']);
Expand Down
30 changes: 30 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Attributes.php
@@ -0,0 +1,30 @@
<?php

/*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity;

class Attributes
{
private $attributes = [];

public function getAttributes()
{
return $this->attributes;
}

public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
}

public function addAttributes(Attribute $attribute)
{
$this->attributes[] = $attribute;
}
}
29 changes: 29 additions & 0 deletions tests/FunctionalTest.php
Expand Up @@ -10,6 +10,7 @@
namespace Dunglas\DoctrineJsonOdm\tests;

use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity\Attribute;
use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity\Attributes;
use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity\Bar;
use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity\Baz;
use Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity\Foo;
Expand Down Expand Up @@ -121,4 +122,32 @@ public function testNestedObjects()
$foo = $manager->find(Foo::class, $foo->getId());
$this->assertEquals($misc, $foo->getMisc());
}

public function testNestedObjectsInNestedObject()
{
$attribute1 = new Attribute();
$attribute1->key = 'attribute1';

$attribute2 = new Attribute();
$attribute2->key = 'attribute2';

$attributes = new Attributes();
$attributes->setAttributes([$attribute1, $attribute2]);

$misc = [$attributes];

$foo = new Foo();
$foo->setName('foo');
$foo->setMisc($misc);

$manager = self::$kernel->getContainer()->get('doctrine')->getManagerForClass(Foo::class);

$manager->persist($foo);
$manager->flush();
$manager->clear();

$foo = $manager->find(Foo::class, $foo->getId());

$this->assertEquals($misc, $foo->getMisc());
}
}

0 comments on commit 47e1383

Please sign in to comment.