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

Skip to content

Commit 2ab3ad0

Browse files
committed
[Serializer] Adopt a different caching approach
1 parent 57c5944 commit 2ab3ad0

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

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

Lines changed: 34 additions & 8 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]>
@@ -62,11 +63,12 @@ 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+
$cacheKey = $this->getCacheKey($propertyName, $context);
67+
if (!isset(self::$denormalizeCache[$class][$cacheKey])) {
68+
self::$denormalizeCache[$class][$cacheKey] = $this->getCacheValueForDenormalization($propertyName, $class, $context);
6769
}
6870

69-
return self::$denormalizeCache[$class][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
71+
return self::$denormalizeCache[$class][$cacheKey] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
7072
}
7173

7274
private function getCacheValueForNormalization($propertyName, string $class)
@@ -88,21 +90,21 @@ private function normalizeFallback($propertyName, string $class = null, string $
8890
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName;
8991
}
9092

91-
private function getCacheValueForDenormalization($propertyName, string $class)
93+
private function getCacheValueForDenormalization($propertyName, string $class, $context)
9294
{
9395
if (!isset(self::$attributesMetadataCache[$class])) {
94-
self::$attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class);
96+
self::$attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class, $context);
9597
}
9698

97-
return self::$attributesMetadataCache[$class][$propertyName] ?? null;
99+
return self::$attributesMetadataCache[$class][$this->getCacheKey($propertyName, $context)] ?? null;
98100
}
99101

100102
private function denormalizeFallback($propertyName, string $class = null, string $format = null, array $context = [])
101103
{
102104
return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName;
103105
}
104106

105-
private function getCacheValueForAttributesMetadata(string $class): array
107+
private function getCacheValueForAttributesMetadata(string $class, $context): array
106108
{
107109
if (!$this->metadataFactory->hasMetadataFor($class)) {
108110
return [];
@@ -116,9 +118,33 @@ private function getCacheValueForAttributesMetadata(string $class): array
116118
continue;
117119
}
118120

119-
$cache[$metadata->getSerializedName()] = $name;
121+
if (!($groups = $metadata->getGroups()) && ($context[AbstractNormalizer::GROUPS] ?? [])) {
122+
continue;
123+
}
124+
if ($groups && !array_intersect($groups, $context[AbstractNormalizer::GROUPS] ?? [])) {
125+
continue;
126+
}
127+
128+
129+
$cache[$this->getCacheKey($metadata->getSerializedName(), $context)] = $name;
120130
}
121131

122132
return $cache;
123133
}
134+
135+
private function getCacheKey($propertyName, array $context): string
136+
{
137+
if (isset($context['cache_key'])) {
138+
return $propertyName.$context['cache_key'];
139+
}
140+
141+
try {
142+
$key = md5(serialize($context[AbstractNormalizer::GROUPS] ?? []));
143+
} catch (\Exception $e) {
144+
$key = '';
145+
} finally {
146+
return $propertyName.$key;
147+
}
148+
149+
}
124150
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public function denormalize($data, $type, $format = null, array $context = [])
330330
$resolvedClass = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);
331331

332332
foreach ($normalizedData as $attribute => $value) {
333-
if ($this->nameConverter && (false !== $allowedAttributes && !\in_array($attribute, $allowedAttributes))) {
333+
if ($this->nameConverter) {
334334
$attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $context);
335335
}
336336

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
}

0 commit comments

Comments
 (0)