Skip to content

Commit

Permalink
bug #19352 [Serializer] Include the format in the cache key (dunglas)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.1 branch (closes #19352).

Discussion
----------

[Serializer] Include the format in the cache key

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Attributes to normalize can vary by format. For instance in API Platform normalized attributes aren't the same in JSON-LD (all attributes) and HAL (relations are not serialized in the document body).
This PR fixes that.

Commits
-------

282eb9c [Serializer] Include the format in the cache key
  • Loading branch information
fabpot committed Jul 13, 2016
2 parents 414d9ef + 282eb9c commit cf691fb
Showing 1 changed file with 6 additions and 5 deletions.
Expand Up @@ -56,7 +56,7 @@ public function supportsNormalization($data, $format = null)
public function normalize($object, $format = null, array $context = array())
{
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($context);
$context['cache_key'] = $this->getCacheKey($format, $context);
}

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

0 comments on commit cf691fb

Please sign in to comment.