@@ -58,7 +58,7 @@ public function supportsNormalization($data, $format = null)
58
58
public function normalize ($ object , $ format = null , array $ context = [])
59
59
{
60
60
if (!isset ($ context ['cache_key ' ])) {
61
- $ context ['cache_key ' ] = $ this ->getCacheKey ($ format , $ context );
61
+ $ context ['cache_key ' ] = $ this ->getAttributeCacheKey ($ format , $ context );
62
62
}
63
63
64
64
if ($ this ->isCircularReference ($ object , $ context )) {
@@ -128,15 +128,13 @@ protected function getAttributes($object, $format = null, array $context)
128
128
return $ allowedAttributes ;
129
129
}
130
130
131
- if (isset ($ context ['attributes ' ])) {
132
- return $ this ->extractAttributes ($ object , $ format , $ context );
133
- }
131
+ $ attributes = $ this ->extractAttributes ($ object , $ format , $ context );
134
132
135
- if (isset ( $ this -> attributesCache [ $ class ]) ) {
136
- return $ this ->attributesCache [$ class ] ;
133
+ if ($ context [ ' cache_key ' ] ) {
134
+ $ this ->attributesCache [$ key ] = $ attributes ;
137
135
}
138
136
139
- return $ this -> attributesCache [ $ class ] = $ this -> extractAttributes ( $ object , $ format , $ context ) ;
137
+ return $ attributes ;
140
138
}
141
139
142
140
/**
@@ -176,7 +174,7 @@ public function supportsDenormalization($data, $type, $format = null)
176
174
public function denormalize ($ data , $ class , $ format = null , array $ context = [])
177
175
{
178
176
if (!isset ($ context ['cache_key ' ])) {
179
- $ context ['cache_key ' ] = $ this ->getCacheKey ($ format , $ context );
177
+ $ context ['cache_key ' ] = $ this ->getAttributeCacheKey ($ format , $ context );
180
178
}
181
179
182
180
$ allowedAttributes = $ this ->getAllowedAttributes ($ class , $ context , true );
@@ -373,17 +371,33 @@ private function isMaxDepthReached(array $attributesMetadata, $class, $attribute
373
371
}
374
372
375
373
/**
376
- * Gets the cache key to use.
374
+ * Overwrite to update the cache key for the child.
375
+ *
376
+ * {@inheritdoc}
377
+ */
378
+ protected function createChildContext (array $ parentContext , $ attribute )
379
+ {
380
+ $ context = parent ::createChildContext ($ parentContext , $ attribute );
381
+ // format is already included in the cache_key of the parent.
382
+ $ context ['cache_key ' ] = $ this ->getAttributeCacheKey ('' , $ context );
383
+
384
+ return $ context ;
385
+ }
386
+
387
+ /**
388
+ * Build the cache key for the attributes cache.
389
+ *
390
+ * The key must be different for every option in the context that could change which attributes should be handled.
377
391
*
378
392
* @param string|null $format
379
393
* @param array $context
380
394
*
381
395
* @return bool|string
382
396
*/
383
- private function getCacheKey ($ format , array $ context )
397
+ private function getAttributeCacheKey ($ format , array $ context )
384
398
{
385
399
try {
386
- return md5 ($ format .serialize ($ context )) ;
400
+ return md5 ($ format .serialize ($ context ). serialize ( $ this -> ignoredAttributes )). serialize ( $ this -> camelizedAttributes ). serialize ( $ this -> callbacks ). $ this -> circularReferenceLimit ;
387
401
} catch (\Exception $ exception ) {
388
402
// The context cannot be serialized, skip the cache
389
403
return false ;
0 commit comments