Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ca4a3f

Browse files
antograssiotfabpot
authored andcommitted
[Serializer] Fix property name usage for denormalization
1 parent ce6332c commit 8ca4a3f

File tree

5 files changed

+109
-15
lines changed

5 files changed

+109
-15
lines changed

src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\NameConverter;
1313

1414
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
15+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
1516

1617
/**
1718
* @author Fabien Bourigault <[email protected]>
@@ -25,11 +26,11 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
2526
*/
2627
private $fallbackNameConverter;
2728

28-
private static $normalizeCache = [];
29+
private $normalizeCache = [];
2930

30-
private static $denormalizeCache = [];
31+
private $denormalizeCache = [];
3132

32-
private static $attributesMetadataCache = [];
33+
private $attributesMetadataCache = [];
3334

3435
public function __construct(ClassMetadataFactoryInterface $metadataFactory, NameConverterInterface $fallbackNameConverter = null)
3536
{
@@ -46,11 +47,11 @@ public function normalize($propertyName, string $class = null, string $format =
4647
return $this->normalizeFallback($propertyName, $class, $format, $context);
4748
}
4849

49-
if (!isset(self::$normalizeCache[$class][$propertyName])) {
50-
self::$normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class);
50+
if (!isset($this->normalizeCache[$class][$propertyName])) {
51+
$this->normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class);
5152
}
5253

53-
return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context);
54+
return $this->normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context);
5455
}
5556

5657
/**
@@ -62,11 +63,11 @@ public function denormalize($propertyName, string $class = null, string $format
6263
return $this->denormalizeFallback($propertyName, $class, $format, $context);
6364
}
6465

65-
if (!isset(self::$denormalizeCache[$class][$propertyName])) {
66-
self::$denormalizeCache[$class][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class);
66+
if (!isset($this->denormalizeCache[$class][$propertyName])) {
67+
$this->denormalizeCache[$class][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class, $context);
6768
}
6869

69-
return self::$denormalizeCache[$class][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
70+
return $this->denormalizeCache[$class][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
7071
}
7172

7273
private function getCacheValueForNormalization($propertyName, string $class)
@@ -88,21 +89,21 @@ private function normalizeFallback($propertyName, string $class = null, string $
8889
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName;
8990
}
9091

91-
private function getCacheValueForDenormalization($propertyName, string $class)
92+
private function getCacheValueForDenormalization($propertyName, string $class, $context)
9293
{
93-
if (!isset(self::$attributesMetadataCache[$class])) {
94-
self::$attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class);
94+
if (!isset($this->attributesMetadataCache[$class])) {
95+
$this->attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class, $context);
9596
}
9697

97-
return self::$attributesMetadataCache[$class][$propertyName] ?? null;
98+
return $this->attributesMetadataCache[$class][$propertyName] ?? null;
9899
}
99100

100101
private function denormalizeFallback($propertyName, string $class = null, string $format = null, array $context = [])
101102
{
102103
return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName;
103104
}
104105

105-
private function getCacheValueForAttributesMetadata(string $class): array
106+
private function getCacheValueForAttributesMetadata(string $class, $context): array
106107
{
107108
if (!$this->metadataFactory->hasMetadataFor($class)) {
108109
return [];
@@ -116,6 +117,14 @@ private function getCacheValueForAttributesMetadata(string $class): array
116117
continue;
117118
}
118119

120+
$groups = $metadata->getGroups();
121+
if (!$groups && ($context[AbstractNormalizer::GROUPS] ?? [])) {
122+
continue;
123+
}
124+
if ($groups && !array_intersect($groups, $context[AbstractNormalizer::GROUPS] ?? [])) {
125+
continue;
126+
}
127+
119128
$cache[$metadata->getSerializedName()] = $name;
120129
}
121130

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
236236
*
237237
* @param object $object
238238
* @param string|null $format
239-
* @param array $context
240239
*
241240
* @return string[]
242241
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
use Symfony\Component\Serializer\Annotation\Groups;
15+
use Symfony\Component\Serializer\Annotation\SerializedName;
16+
17+
/**
18+
* @author Anthony GRASSIOT <[email protected]>
19+
*/
20+
class OtherSerializedNameDummy
21+
{
22+
/**
23+
* @Groups({"a"})
24+
*/
25+
private $buz;
26+
27+
public function setBuz($buz)
28+
{
29+
$this->buz = $buz;
30+
}
31+
32+
public function getBuz()
33+
{
34+
return $this->buz;
35+
}
36+
37+
/**
38+
* @Groups({"b"})
39+
* @SerializedName("buz")
40+
*/
41+
public function getBuzForExport()
42+
{
43+
return $this->buz.' Rocks';
44+
}
45+
}

src/Symfony/Component/Serializer/Tests/NameConverter/MetadataAwareNameConverterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
1919
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
2020
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
21+
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
2122
use Symfony\Component\Serializer\Tests\Fixtures\SerializedNameDummy;
2223

2324
/**
@@ -115,4 +116,26 @@ public function fallbackAttributeProvider()
115116
[0, 0],
116117
];
117118
}
119+
120+
/**
121+
* @dataProvider attributeAndContextProvider
122+
*/
123+
public function testDenormalizeWithGroups($expected, $propertyName, $context = [])
124+
{
125+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
126+
127+
$nameConverter = new MetadataAwareNameConverter($classMetadataFactory);
128+
129+
$this->assertEquals($expected, $nameConverter->denormalize($propertyName, OtherSerializedNameDummy::class, null, $context));
130+
}
131+
132+
public function attributeAndContextProvider()
133+
{
134+
return [
135+
['buz', 'buz', ['groups' => ['a']]],
136+
['buzForExport', 'buz', ['groups' => ['b']]],
137+
['buz', 'buz', ['groups' => ['c']]],
138+
['buz', 'buz', []],
139+
];
140+
}
118141
}

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
3333
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
3434
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
35+
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
3536
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
3637
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
3738
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksObject;
@@ -481,6 +482,23 @@ public function testGroupsDenormalizeWithNameConverter()
481482
);
482483
}
483484

485+
public function testGroupsDenormalizeWithMetaDataNameConverter()
486+
{
487+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
488+
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
489+
$this->normalizer->setSerializer($this->serializer);
490+
491+
$obj = new OtherSerializedNameDummy();
492+
$obj->setBuz('Aldrin');
493+
494+
$this->assertEquals(
495+
$obj,
496+
$this->normalizer->denormalize([
497+
'buz' => 'Aldrin',
498+
], 'Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy', null, [ObjectNormalizer::GROUPS => ['a']])
499+
);
500+
}
501+
484502
// ignored attributes
485503

486504
protected function getNormalizerForIgnoredAttributes(): ObjectNormalizer

0 commit comments

Comments
 (0)