@@ -58,7 +58,7 @@ public function supportsNormalization($data, $format = null)
5858public function normalize ($ object ,$ format =null ,array $ context = [])
5959 {
6060if (!isset ($ context ['cache_key ' ])) {
61- $ context ['cache_key ' ] =$ this ->getCacheKey ($ format ,$ context );
61+ $ context ['cache_key ' ] =$ this ->getAttributeCacheKey ($ format ,$ context );
6262 }
6363
6464if ($ this ->isCircularReference ($ object ,$ context )) {
@@ -128,15 +128,13 @@ protected function getAttributes($object, $format = null, array $context)
128128return $ allowedAttributes ;
129129 }
130130
131- if (isset ($ context ['attributes ' ])) {
132- return $ this ->extractAttributes ($ object ,$ format ,$ context );
133- }
131+ $ attributes =$ this ->extractAttributes ($ object ,$ format ,$ context );
134132
135- if (isset ( $ this -> attributesCache [ $ class ]) ) {
136- return $ this ->attributesCache [$ class ] ;
133+ if ($ context [ ' cache_key ' ] ) {
134+ $ this ->attributesCache [$ key ] = $ attributes ;
137135 }
138136
139- return $ this -> attributesCache [ $ class ] = $ this -> extractAttributes ( $ object , $ format , $ context ) ;
137+ return $ attributes ;
140138 }
141139
142140/**
@@ -176,7 +174,7 @@ public function supportsDenormalization($data, $type, $format = null)
176174public function denormalize ($ data ,$ class ,$ format =null ,array $ context = [])
177175 {
178176if (!isset ($ context ['cache_key ' ])) {
179- $ context ['cache_key ' ] =$ this ->getCacheKey ($ format ,$ context );
177+ $ context ['cache_key ' ] =$ this ->getAttributeCacheKey ($ format ,$ context );
180178 }
181179
182180$ allowedAttributes =$ this ->getAllowedAttributes ($ class ,$ context ,true );
@@ -373,17 +371,33 @@ private function isMaxDepthReached(array $attributesMetadata, $class, $attribute
373371 }
374372
375373/**
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.
377391 *
378392 * @param string|null $format
379393 * @param array $context
380394 *
381395 * @return bool|string
382396 */
383- private function getCacheKey ($ format ,array $ context )
397+ private function getAttributeCacheKey ($ format ,array $ context )
384398 {
385399try {
386- return md5 ($ format .serialize ($ context ));
400+ return md5 ($ format .serialize ($ context ). serialize ( $ this -> ignoredAttributes )). serialize ( $ this -> camelizedAttributes );
387401 }catch (\Exception $ exception ) {
388402// The context cannot be serialized, skip the cache
389403return false ;