bug #34035 [Serializer] Fix property name usage for denormalization (antograssiot)
This PR was squashed before being merged into the 4.3 branch (closes #34035).
Discussion
----------
[Serializer] Fix property name usage for denormalization
| Q | A
| ------------- | ---
| Branch? | 4.3
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets |
| License | MIT
| Doc PR |
Using the `@SerializedName()` and passing it an existing property name affects the deserialization even if `@Groups()` are not supposed to be involved.
## How to reproduce
Given the following class
```php
class Foo
{
/**
* @Group("list")
*/
private $bar;
public function setBar($bar)
{
$this->bar = $bar;
}
public function getBar()
{
return $this->bar;
}
/**
* @Groups({"list:export"})
* @SerializedName("bar")
*/
public function getBarForExport()
{
return $this->bar.' Rocks';
}
}
```
This allow us to change the content of the property based on the normalization context.
```php
$obj = new Foo();
$obj->setBar('Api Platform');
$data = $normalizer->normalize($obj, null, ['groups' => ["list"]]);
// $data => ['bar' => 'Api Platform'] as expected
$data = $normalizer->normalize($obj, null, ['groups' => ["list:export"]]);
// $data => ['bar' => 'Api Platform Rocks'] as expected
$obj = $normalizer->denormalize(['bar' => 'Api Platform'], Foo::class, null, ['groups' => ['list']]);
// $obj->getBar() would return null instead of 'Api Platform' as expected.
```
Commits
-------
8ca4a3f345
[Serializer] Fix property name usage for denormalization
This commit is contained in:
commit
22976d0f54
@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\Serializer\NameConverter;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
|
||||
/**
|
||||
* @author Fabien Bourigault <bourigaultfabien@gmail.com>
|
||||
@ -25,11 +26,11 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
|
||||
*/
|
||||
private $fallbackNameConverter;
|
||||
|
||||
private static $normalizeCache = [];
|
||||
private $normalizeCache = [];
|
||||
|
||||
private static $denormalizeCache = [];
|
||||
private $denormalizeCache = [];
|
||||
|
||||
private static $attributesMetadataCache = [];
|
||||
private $attributesMetadataCache = [];
|
||||
|
||||
public function __construct(ClassMetadataFactoryInterface $metadataFactory, NameConverterInterface $fallbackNameConverter = null)
|
||||
{
|
||||
@ -46,11 +47,11 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
|
||||
return $this->normalizeFallback($propertyName, $class, $format, $context);
|
||||
}
|
||||
|
||||
if (!isset(self::$normalizeCache[$class][$propertyName])) {
|
||||
self::$normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class);
|
||||
if (!isset($this->normalizeCache[$class][$propertyName])) {
|
||||
$this->normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class);
|
||||
}
|
||||
|
||||
return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context);
|
||||
return $this->normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -62,11 +63,11 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
|
||||
return $this->denormalizeFallback($propertyName, $class, $format, $context);
|
||||
}
|
||||
|
||||
if (!isset(self::$denormalizeCache[$class][$propertyName])) {
|
||||
self::$denormalizeCache[$class][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class);
|
||||
if (!isset($this->denormalizeCache[$class][$propertyName])) {
|
||||
$this->denormalizeCache[$class][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class, $context);
|
||||
}
|
||||
|
||||
return self::$denormalizeCache[$class][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
|
||||
return $this->denormalizeCache[$class][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
|
||||
}
|
||||
|
||||
private function getCacheValueForNormalization($propertyName, string $class)
|
||||
@ -88,13 +89,13 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
|
||||
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName;
|
||||
}
|
||||
|
||||
private function getCacheValueForDenormalization($propertyName, string $class)
|
||||
private function getCacheValueForDenormalization($propertyName, string $class, $context)
|
||||
{
|
||||
if (!isset(self::$attributesMetadataCache[$class])) {
|
||||
self::$attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class);
|
||||
if (!isset($this->attributesMetadataCache[$class])) {
|
||||
$this->attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class, $context);
|
||||
}
|
||||
|
||||
return self::$attributesMetadataCache[$class][$propertyName] ?? null;
|
||||
return $this->attributesMetadataCache[$class][$propertyName] ?? null;
|
||||
}
|
||||
|
||||
private function denormalizeFallback($propertyName, string $class = null, string $format = null, array $context = [])
|
||||
@ -102,7 +103,7 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
|
||||
return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName;
|
||||
}
|
||||
|
||||
private function getCacheValueForAttributesMetadata(string $class): array
|
||||
private function getCacheValueForAttributesMetadata(string $class, $context): array
|
||||
{
|
||||
if (!$this->metadataFactory->hasMetadataFor($class)) {
|
||||
return [];
|
||||
@ -116,6 +117,14 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
$groups = $metadata->getGroups();
|
||||
if (!$groups && ($context[AbstractNormalizer::GROUPS] ?? [])) {
|
||||
continue;
|
||||
}
|
||||
if ($groups && !array_intersect($groups, $context[AbstractNormalizer::GROUPS] ?? [])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$cache[$metadata->getSerializedName()] = $name;
|
||||
}
|
||||
|
||||
|
@ -236,7 +236,6 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
|
||||
*
|
||||
* @param object $object
|
||||
* @param string|null $format
|
||||
* @param array $context
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
|
@ -0,0 +1,45 @@
|
||||
<?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;
|
||||
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
use Symfony\Component\Serializer\Annotation\SerializedName;
|
||||
|
||||
/**
|
||||
* @author Anthony GRASSIOT <antograssiot@free.fr>
|
||||
*/
|
||||
class OtherSerializedNameDummy
|
||||
{
|
||||
/**
|
||||
* @Groups({"a"})
|
||||
*/
|
||||
private $buz;
|
||||
|
||||
public function setBuz($buz)
|
||||
{
|
||||
$this->buz = $buz;
|
||||
}
|
||||
|
||||
public function getBuz()
|
||||
{
|
||||
return $this->buz;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Groups({"b"})
|
||||
* @SerializedName("buz")
|
||||
*/
|
||||
public function getBuzForExport()
|
||||
{
|
||||
return $this->buz.' Rocks';
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@ use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
|
||||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\SerializedNameDummy;
|
||||
|
||||
/**
|
||||
@ -115,4 +116,26 @@ final class MetadataAwareNameConverterTest extends TestCase
|
||||
[0, 0],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributeAndContextProvider
|
||||
*/
|
||||
public function testDenormalizeWithGroups($expected, $propertyName, $context = [])
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
|
||||
$nameConverter = new MetadataAwareNameConverter($classMetadataFactory);
|
||||
|
||||
$this->assertEquals($expected, $nameConverter->denormalize($propertyName, OtherSerializedNameDummy::class, null, $context));
|
||||
}
|
||||
|
||||
public function attributeAndContextProvider()
|
||||
{
|
||||
return [
|
||||
['buz', 'buz', ['groups' => ['a']]],
|
||||
['buzForExport', 'buz', ['groups' => ['b']]],
|
||||
['buz', 'buz', ['groups' => ['c']]],
|
||||
['buz', 'buz', []],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,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\OtherSerializedNameDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
|
||||
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
|
||||
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksObject;
|
||||
@ -481,6 +482,23 @@ class ObjectNormalizerTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testGroupsDenormalizeWithMetaDataNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new OtherSerializedNameDummy();
|
||||
$obj->setBuz('Aldrin');
|
||||
|
||||
$this->assertEquals(
|
||||
$obj,
|
||||
$this->normalizer->denormalize([
|
||||
'buz' => 'Aldrin',
|
||||
], 'Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy', null, [ObjectNormalizer::GROUPS => ['a']])
|
||||
);
|
||||
}
|
||||
|
||||
// ignored attributes
|
||||
|
||||
protected function getNormalizerForIgnoredAttributes(): ObjectNormalizer
|
||||
|
Reference in New Issue
Block a user