-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Respect ignored attributes in cache key of normalizer #30907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,7 @@ public function normalize($object, $format = null, array $context = []) | |
throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attribute)); | ||
} | ||
|
||
$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute))); | ||
$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute, $format))); | ||
} | ||
|
||
return $data; | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -276,7 +274,7 @@ private function validateAndDenormalize($currentClass, $attribute, $data, $forma | |
throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer', $attribute, $class)); | ||
} | ||
|
||
$childContext = $this->createChildContext($context, $attribute); | ||
$childContext = $this->createChildContext($context, $attribute, $format); | ||
if ($this->serializer->supportsDenormalization($data, $class, $format, $childContext)) { | ||
return $this->serializer->denormalize($data, $class, $format, $childContext); | ||
} | ||
|
@@ -373,7 +371,32 @@ private function isMaxDepthReached(array $attributesMetadata, $class, $attribute | |
} | ||
|
||
/** | ||
* Gets the cache key to use. | ||
* Overwritten to update the cache key for the child. | ||
* | ||
* We must not mix up the attribute cache between parent and children. | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
protected function createChildContext(array $parentContext, $attribute/*, string $format = null */) | ||
{ | ||
if (\func_num_args() >= 3) { | ||
$format = \func_get_arg(2); | ||
} else { | ||
// will be deprecated in version 4 | ||
$format = null; | ||
} | ||
|
||
$context = parent::createChildContext($parentContext, $attribute, $format); | ||
// format is already included in the cache_key of the parent. | ||
$context['cache_key'] = $this->getCacheKey($format, $context); | ||
|
||
return $context; | ||
} | ||
|
||
/** | ||
* Builds 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 | ||
|
@@ -382,8 +405,13 @@ private function isMaxDepthReached(array $attributesMetadata, $class, $attribute | |
*/ | ||
private function getCacheKey($format, array $context) | ||
{ | ||
unset($context['cache_key']); // avoid artificially different keys | ||
try { | ||
return md5($format.serialize($context)); | ||
return md5($format.serialize([ | ||
'context' => $context, | ||
'ignored' => $this->ignoredAttributes, | ||
'camelized' => $this->camelizedAttributes, | ||
])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i hope i caught all normalizer state that could influence which attributes are available and not too many. things like circular reference or callbacks should not change which attributes are available i guess? (serialize one array rather than concat several serialize result, assuming that gives better performance) |
||
} catch (\Exception $exception) { | ||
// The context cannot be serialized, skip the cache | ||
return false; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we previously skipped the cache if attributes are defined in the context. now we use the attributes as part of the cache key.