Skip to content

Commit 0b35725

Browse files
authored
Merge pull request #1894 from bendavies/normalizer-cache-keys
fix normalizer cache key generation
2 parents 309e0ff + f9706ea commit 0b35725

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/Hal/Serializer/ItemNormalizer.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public function supportsNormalization($data, $format = null)
4343
*/
4444
public function normalize($object, $format = null, array $context = [])
4545
{
46-
$context['cache_key'] = $this->getHalCacheKey($format, $context);
46+
if (!isset($context['cache_key'])) {
47+
$context['cache_key'] = $this->getHalCacheKey($format, $context);
48+
}
49+
4750
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);
4851
$context = $this->initContext($resourceClass, $context);
4952
$context['iri'] = $this->iriConverter->getIriFromItem($object);
@@ -99,8 +102,10 @@ protected function getAttributes($object, $format = null, array $context)
99102
*/
100103
private function getComponents($object, string $format = null, array $context)
101104
{
102-
if (false !== $context['cache_key'] && isset($this->componentsCache[$context['cache_key']])) {
103-
return $this->componentsCache[$context['cache_key']];
105+
$cacheKey = \get_class($object).'-'.$context['cache_key'];
106+
107+
if (isset($this->componentsCache[$cacheKey])) {
108+
return $this->componentsCache[$cacheKey];
104109
}
105110

106111
$attributes = parent::getAttributes($object, $format, $context);
@@ -142,7 +147,7 @@ private function getComponents($object, string $format = null, array $context)
142147
}
143148

144149
if (false !== $context['cache_key']) {
145-
$this->componentsCache[$context['cache_key']] = $components;
150+
$this->componentsCache[$cacheKey] = $components;
146151
}
147152

148153
return $components;

src/JsonApi/Serializer/ItemNormalizer.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public function supportsNormalization($data, $format = null)
6060
*/
6161
public function normalize($object, $format = null, array $context = [])
6262
{
63-
$context['cache_key'] = $this->getJsonApiCacheKey($format, $context);
63+
if (!isset($context['cache_key'])) {
64+
$context['cache_key'] = $this->getJsonApiCacheKey($format, $context);
65+
}
6466

6567
// Get and populate attributes data
6668
$objectAttributesData = parent::normalize($object, $format, $context);
@@ -221,8 +223,10 @@ protected function isAllowedAttribute($classOrObject, $attribute, $format = null
221223
*/
222224
private function getComponents($object, string $format = null, array $context)
223225
{
224-
if (isset($this->componentsCache[$context['cache_key']])) {
225-
return $this->componentsCache[$context['cache_key']];
226+
$cacheKey = \get_class($object).'-'.$context['cache_key'];
227+
228+
if (isset($this->componentsCache[$cacheKey])) {
229+
return $this->componentsCache[$cacheKey];
226230
}
227231

228232
$attributes = parent::getAttributes($object, $format, $context);
@@ -267,7 +271,11 @@ private function getComponents($object, string $format = null, array $context)
267271
$components['relationships'][] = $relation;
268272
}
269273

270-
return $this->componentsCache[$context['cache_key']] = $components;
274+
if (false !== $context['cache_key']) {
275+
$this->componentsCache[$cacheKey] = $components;
276+
}
277+
278+
return $components;
271279
}
272280

273281
/**

0 commit comments

Comments
 (0)