[Serializer] Include the format in the cache key

This commit is contained in:
Kévin Dunglas 2016-07-12 23:26:38 +02:00 committed by Fabien Potencier
parent 414d9efd02
commit 282eb9c410

View File

@ -56,7 +56,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
public function normalize($object, $format = null, array $context = array()) public function normalize($object, $format = null, array $context = array())
{ {
if (!isset($context['cache_key'])) { if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($context); $context['cache_key'] = $this->getCacheKey($format, $context);
} }
if ($this->isCircularReference($object, $context)) { if ($this->isCircularReference($object, $context)) {
@ -169,7 +169,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
public function denormalize($data, $class, $format = null, array $context = array()) public function denormalize($data, $class, $format = null, array $context = array())
{ {
if (!isset($context['cache_key'])) { if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($context); $context['cache_key'] = $this->getCacheKey($format, $context);
} }
$allowedAttributes = $this->getAllowedAttributes($class, $context, true); $allowedAttributes = $this->getAllowedAttributes($class, $context, true);
$normalizedData = $this->prepareForDenormalization($data); $normalizedData = $this->prepareForDenormalization($data);
@ -336,14 +336,15 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
/** /**
* Gets the cache key to use. * Gets the cache key to use.
* *
* @param array $context * @param string|null $format
* @param array $context
* *
* @return bool|string * @return bool|string
*/ */
private function getCacheKey(array $context) private function getCacheKey($format, array $context)
{ {
try { try {
return md5(serialize($context)); return md5($format.serialize($context));
} catch (\Exception $exception) { } catch (\Exception $exception) {
// The context cannot be serialized, skip the cache // The context cannot be serialized, skip the cache
return false; return false;