Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Hackday][Serializer] Deserialization ignores argument type hint from…
… phpdoc for array in constructor argument
- Loading branch information
Showing
3 changed files
with
162 additions
and
12 deletions.
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
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 |
---|---|---|
|
@@ -304,6 +304,18 @@ private function validateAndDenormalize($currentClass, $attribute, $data, $forma | |
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), \gettype($data))); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null) | ||
{ | ||
if (null === $this->propertyTypeExtractor || null === $types = $this->propertyTypeExtractor->getTypes($class->getName(), $parameterName)) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
xabbuh
Member
|
||
return parent::denormalizeParameter($class, $parameter, $parameterName, $parameterData, $context, $format); | ||
} | ||
|
||
return $this->validateAndDenormalize($class->getName(), $parameterName, $parameterData, $format, $context); | ||
} | ||
|
||
/** | ||
* Sets an attribute and apply the name converter if necessary. | ||
* | ||
|
128 changes: 128 additions & 0 deletions
128
src/Symfony/Component/Serializer/Tests/DeserializeNestedArrayOfObjectsTest.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,128 @@ | ||
<?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\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 array( | ||
//from property PhpDoc | ||
array(Zoo::class), | ||
//from argument constructor PhpDoc | ||
array(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 = array(); | ||
|
||
/** | ||
* @return Animal[] | ||
*/ | ||
public function getAnimals() | ||
{ | ||
return $this->animals; | ||
} | ||
|
||
/** | ||
* @param Animal[] $animals | ||
*/ | ||
public function setAnimals(array $animals) | ||
{ | ||
$this->animals = $animals; | ||
} | ||
} | ||
|
||
class ZooImmutable | ||
{ | ||
/** @var Animal[] */ | ||
private $animals = array(); | ||
|
||
/** | ||
* @param Animal[] $animals | ||
*/ | ||
public function __construct(array $animals = array()) | ||
{ | ||
$this->animals = $animals; | ||
} | ||
|
||
/** | ||
* @return Animal[] | ||
*/ | ||
public function getAnimals() | ||
{ | ||
return $this->animals; | ||
} | ||
} | ||
|
||
class Animal | ||
{ | ||
/** @var string */ | ||
private $name; | ||
|
||
public function __construct() | ||
{ | ||
echo ''; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @param string|null $name | ||
*/ | ||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
} |
What should I do in this case? All my request DTO's are build this way. I am using fos_rest param converter + symfony serializer... 😠