Last active
October 11, 2018 23:23
-
-
Save karser/959a276b5fb9a616158f47730c1225a6 to your computer and use it in GitHub Desktop.
[Serializer] Deserialization ignores argument type hint from phpdoc for array in constructor argument https://github.com/symfony/symfony/issues/28825
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
<?php | |
namespace Symfony\Component\Serializer\Tests; | |
use PHPUnit\Framework\TestCase; | |
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; | |
use Symfony\Component\Serializer\Encoder\JsonEncoder; | |
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; | |
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; | |
use Symfony\Component\Serializer\Serializer; | |
class DeserializeNestedArrayOfObjectsTest extends TestCase | |
{ | |
public function provider() | |
{ | |
return [ | |
//from property PhpDoc | |
[Zoo::class], | |
//from argument constructor PhpDoc | |
[ZooImmutable::class], | |
]; | |
} | |
/** | |
* @dataProvider provider | |
*/ | |
public function testPropertyPhpDoc($class) | |
{ | |
//GIVEN | |
$json = <<<EOF | |
{ | |
"animals": [ | |
{"name": "Bug"} | |
] | |
} | |
EOF; | |
$serializer = new Serializer(array( | |
new ObjectNormalizer(null,null, null, new PhpDocExtractor()), | |
new ArrayDenormalizer(), | |
), array('json' => new JsonEncoder())); | |
//WHEN | |
/** @var Zoo $zoo */ | |
$zoo = $serializer->deserialize($json, $class, 'json'); | |
//THEN | |
self::assertCount(1, $zoo->getAnimals()); | |
self::assertInstanceOf(Animal::class, $zoo->getAnimals()[0]); | |
} | |
} | |
class Zoo { | |
/** @var Animal[] */ | |
private $animals = []; | |
/** | |
* @return Animal[] | |
*/ | |
public function getAnimals(): array | |
{ | |
return $this->animals; | |
} | |
/** | |
* @param Animal[] $animals | |
*/ | |
public function setAnimals(array $animals): void | |
{ | |
$this->animals = $animals; | |
} | |
} | |
class ZooImmutable { | |
/** @var Animal[] */ | |
private $animals = []; | |
/** | |
* @param Animal[] $animals | |
*/ | |
public function __construct(array $animals = []) | |
{ | |
$this->animals = $animals; | |
} | |
/** | |
* @return Animal[] | |
*/ | |
public function getAnimals(): array | |
{ | |
return $this->animals; | |
} | |
} | |
class Animal { | |
/** @var string */ | |
private $name; | |
/** | |
* @return string | |
*/ | |
public function getName(): ?string | |
{ | |
return $this->name; | |
} | |
/** | |
* @param string $name | |
*/ | |
public function setName(?string $name): void | |
{ | |
$this->name = $name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment