Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 100fe8d

Browse files
committed
avoid unnecessary cache key changes
1 parent e43accb commit 100fe8d

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara
397397
}
398398
$parameterClass = $parameter->getClass()->getName();
399399

400-
return $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName));
400+
return $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName, $format));
401401
}
402402

403403
return $parameterData;
@@ -407,14 +407,15 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara
407407
}
408408

409409
/**
410-
* @param array $parentContext
411-
* @param string $attribute
410+
* @param array $parentContext
411+
* @param string $attribute Attribute name
412+
* @param string|null $format
412413
*
413414
* @return array
414415
*
415416
* @internal
416417
*/
417-
protected function createChildContext(array $parentContext, $attribute)
418+
protected function createChildContext(array $parentContext, $attribute/*, string $format = null */)
418419
{
419420
if (isset($parentContext[self::ATTRIBUTES][$attribute])) {
420421
$parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute];

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function supportsNormalization($data, $format = null)
5858
public function normalize($object, $format = null, array $context = [])
5959
{
6060
if (!isset($context['cache_key'])) {
61-
$context['cache_key'] = $this->getAttributeCacheKey($format, $context);
61+
$context['cache_key'] = $this->getCacheKey($format, $context);
6262
}
6363

6464
if ($this->isCircularReference($object, $context)) {
@@ -94,7 +94,7 @@ public function normalize($object, $format = null, array $context = [])
9494
throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attribute));
9595
}
9696

97-
$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute)));
97+
$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute, $format)));
9898
}
9999

100100
return $data;
@@ -174,7 +174,7 @@ public function supportsDenormalization($data, $type, $format = null)
174174
public function denormalize($data, $class, $format = null, array $context = [])
175175
{
176176
if (!isset($context['cache_key'])) {
177-
$context['cache_key'] = $this->getAttributeCacheKey($format, $context);
177+
$context['cache_key'] = $this->getCacheKey($format, $context);
178178
}
179179

180180
$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
@@ -274,7 +274,7 @@ private function validateAndDenormalize($currentClass, $attribute, $data, $forma
274274
throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer', $attribute, $class));
275275
}
276276

277-
$childContext = $this->createChildContext($context, $attribute);
277+
$childContext = $this->createChildContext($context, $attribute, $format);
278278
if ($this->serializer->supportsDenormalization($data, $class, $format, $childContext)) {
279279
return $this->serializer->denormalize($data, $class, $format, $childContext);
280280
}
@@ -377,11 +377,18 @@ private function isMaxDepthReached(array $attributesMetadata, $class, $attribute
377377
*
378378
* {@inheritdoc}
379379
*/
380-
protected function createChildContext(array $parentContext, $attribute)
380+
protected function createChildContext(array $parentContext, $attribute/*, string $format = null */)
381381
{
382-
$context = parent::createChildContext($parentContext, $attribute);
382+
if (\func_num_args() >= 3) {
383+
$format = \func_get_arg(2);
384+
} else {
385+
// will be deprecated in version 4
386+
$format = null;
387+
}
388+
389+
$context = parent::createChildContext($parentContext, $attribute, $format);
383390
// format is already included in the cache_key of the parent.
384-
$context['cache_key'] = $this->getAttributeCacheKey('', $context);
391+
$context['cache_key'] = $this->getCacheKey($format, $context);
385392

386393
return $context;
387394
}
@@ -396,8 +403,9 @@ protected function createChildContext(array $parentContext, $attribute)
396403
*
397404
* @return bool|string
398405
*/
399-
private function getAttributeCacheKey($format, array $context)
406+
private function getCacheKey($format, array $context)
400407
{
408+
unset($context['cache_key']); // avoid artificially different keys
401409
try {
402410
return md5($format.serialize([
403411
'context' => $context,

0 commit comments

Comments
 (0)