Skip to content

Commit

Permalink
[Serializer] Respect ignored attributes and camelcased attributes in …
Browse files Browse the repository at this point in the history
…cache key and update cache_key in nested structures
  • Loading branch information
dbu committed Apr 6, 2019
1 parent 69058e3 commit 7b64d88
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
Expand Up @@ -58,7 +58,7 @@ public function supportsNormalization($data, $format = null)
public function normalize($object, $format = null, array $context = [])
{
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($format, $context);
$context['cache_key'] = $this->getAttributeCacheKey($format, $context);
}

if ($this->isCircularReference($object, $context)) {
Expand Down Expand Up @@ -128,15 +128,13 @@ protected function getAttributes($object, $format = null, array $context)
return $allowedAttributes;
}

if (isset($context['attributes'])) {
return $this->extractAttributes($object, $format, $context);
}
$attributes = $this->extractAttributes($object, $format, $context);

if (isset($this->attributesCache[$class])) {
return $this->attributesCache[$class];
if ($context['cache_key']) {
$this->attributesCache[$key] = $attributes;
}

return $this->attributesCache[$class] = $this->extractAttributes($object, $format, $context);
return $attributes;
}

/**
Expand Down Expand Up @@ -176,7 +174,7 @@ public function supportsDenormalization($data, $type, $format = null)
public function denormalize($data, $class, $format = null, array $context = [])
{
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($format, $context);
$context['cache_key'] = $this->getAttributeCacheKey($format, $context);
}

$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
Expand Down Expand Up @@ -373,17 +371,33 @@ private function isMaxDepthReached(array $attributesMetadata, $class, $attribute
}

/**
* Gets the cache key to use.
* Overwrite to update the cache key for the child.
*
* {@inheritdoc}
*/
protected function createChildContext(array $parentContext, $attribute)
{
$context = parent::createChildContext($parentContext, $attribute);
// format is already included in the cache_key of the parent.
$context['cache_key'] = $this->getAttributeCacheKey('', $context);

return $context;
}

/**
* Build the cache key for the attributes cache.
*
* The key must be different for every option in the context that could change which attributes should be handled.
*
* @param string|null $format
* @param array $context
*
* @return bool|string
*/
private function getCacheKey($format, array $context)
private function getAttributeCacheKey($format, array $context)
{
try {
return md5($format.serialize($context));
return md5($format.serialize($context).serialize($this->ignoredAttributes)).serialize($this->camelizedAttributes);
} catch (\Exception $exception) {
// The context cannot be serialized, skip the cache
return false;
Expand Down
Expand Up @@ -356,6 +356,16 @@ public function testIgnoredAttributes()
['fooBar' => 'foobar'],
$this->normalizer->normalize($obj, 'any')
);

$this->normalizer->setIgnoredAttributes(['foo', 'baz', 'camelCase', 'object']);

$this->assertEquals(
[
'fooBar' => 'foobar',
'bar' => 'bar',
],
$this->normalizer->normalize($obj, 'any')
);
}

public function testIgnoredAttributesDenormalize()
Expand Down

0 comments on commit 7b64d88

Please sign in to comment.