[Serializer] fix MetadataAwareNameConverter break denormalization

This commit is contained in:
Bruno MATEU 2018-10-17 17:16:10 +02:00
parent dd432c4f1e
commit faf8b009dc
3 changed files with 60 additions and 1 deletions

View File

@ -75,7 +75,12 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
return null;
}
return $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata()[$propertyName]->getSerializedName() ?? null;
$attributesMetadata = $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata();
if (!isset($attributesMetadata[$propertyName])) {
return null;
}
return $attributesMetadata[$propertyName]->getSerializedName() ?? null;
}
private function normalizeFallback(string $propertyName, string $class = null, string $format = null, array $context = array()): string

View File

@ -0,0 +1,31 @@
<?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\Fixtures;
class NotSerializedConstructorArgumentDummy
{
private $bar;
public function __construct($foo)
{
}
public function getBar()
{
return $this->bar;
}
public function setBar($bar)
{
$this->bar = $bar;
}
}

View File

@ -20,6 +20,7 @@ use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
@ -29,6 +30,7 @@ use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
use Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy;
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
/**
@ -365,6 +367,27 @@ class ObjectNormalizerTest extends TestCase
);
}
public function testMetadataAwareNameConvertorWithNotSerializedConstructorParameter()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
$this->normalizer->setSerializer($this->serializer);
$obj = new NotSerializedConstructorArgumentDummy('buz');
$obj->setBar('xyz');
$this->assertEquals(
$obj,
$this->normalizer->denormalize(array('bar' => 'xyz'),
'Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy',
null,
array(ObjectNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS => array(
'Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy' => array('foo' => 'buz'),
))
)
);
}
/**
* @dataProvider provideCallbacks
*/