bug #29513 [Hackday][Serializer] Deserialization ignores argument type hint from phpdoc for array in constructor argument (karser)
This PR was squashed before being merged into the 3.4 branch (closes #29513).
Discussion
----------
[Hackday][Serializer] Deserialization ignores argument type hint from phpdoc for array in constructor argument
| Q | A
| ------------- | ---
| Branch? | 3.4 and up to 4.2
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #28825
| License | MIT
This is a fix of #28825 big thanks @dunglas and @xabbuh
Commits
-------
8741d00d6e
[Hackday][Serializer] Deserialization ignores argument type hint from phpdoc for array in constructor argument
This commit is contained in:
commit
326ebaabaa
@ -358,20 +358,9 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
||||
unset($data[$key]);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
if (null !== $constructorParameter->getClass()) {
|
||||
if (!$this->serializer instanceof DenormalizerInterface) {
|
||||
throw new LogicException(sprintf('Cannot create an instance of %s from serialized data because the serializer inject in "%s" is not a denormalizer', $constructorParameter->getClass(), static::class));
|
||||
}
|
||||
$parameterClass = $constructorParameter->getClass()->getName();
|
||||
$parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $paramName));
|
||||
}
|
||||
} catch (\ReflectionException $e) {
|
||||
throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e);
|
||||
}
|
||||
|
||||
// Don't run set for a parameter passed to the constructor
|
||||
$params[] = $parameterData;
|
||||
$params[] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $context, $format);
|
||||
unset($data[$key]);
|
||||
} elseif ($constructorParameter->isDefaultValueAvailable()) {
|
||||
$params[] = $constructorParameter->getDefaultValue();
|
||||
@ -390,6 +379,27 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
||||
return new $class();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null)
|
||||
{
|
||||
try {
|
||||
if (null !== $parameter->getClass()) {
|
||||
if (!$this->serializer instanceof DenormalizerInterface) {
|
||||
throw new LogicException(sprintf('Cannot create an instance of %s from serialized data because the serializer inject in "%s" is not a denormalizer', $parameter->getClass(), static::class));
|
||||
}
|
||||
$parameterClass = $parameter->getClass()->getName();
|
||||
|
||||
return $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName));
|
||||
}
|
||||
|
||||
return $parameterData;
|
||||
} catch (\ReflectionException $e) {
|
||||
throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $parameterName), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $parentContext
|
||||
* @param string $attribute
|
||||
|
@ -304,6 +304,18 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
|
||||
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)) {
|
||||
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.
|
||||
*
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user