From be2e3fac2536bdfdfec9711b9ab715c001d458cc Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Mon, 8 Apr 2019 10:04:19 +0200 Subject: [PATCH] avoid unnecessary cache key changes --- .../Normalizer/AbstractNormalizer.php | 9 ++++--- .../Normalizer/AbstractObjectNormalizer.php | 25 +++++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index ef1166a493497..31811b2c7b59f 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -397,7 +397,7 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara } $parameterClass = $parameter->getClass()->getName(); - return $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName)); + return $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName, $format)); } return $parameterData; @@ -407,14 +407,15 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara } /** - * @param array $parentContext - * @param string $attribute + * @param array $parentContext + * @param string $attribute Attribute name + * @param string|null $format * * @return array * * @internal */ - protected function createChildContext(array $parentContext, $attribute) + protected function createChildContext(array $parentContext, $attribute/*, ?string $format*/) { if (isset($parentContext[self::ATTRIBUTES][$attribute])) { $parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute]; diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index 54497216802f1..4d65568d2e5c2 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -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->getAttributeCacheKey($format, $context); + $context['cache_key'] = $this->getCacheKey($format, $context); } if ($this->isCircularReference($object, $context)) { @@ -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; @@ -174,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->getAttributeCacheKey($format, $context); + $context['cache_key'] = $this->getCacheKey($format, $context); } $allowedAttributes = $this->getAllowedAttributes($class, $context, true); @@ -274,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); } @@ -377,11 +377,19 @@ private function isMaxDepthReached(array $attributesMetadata, $class, $attribute * * {@inheritdoc} */ - protected function createChildContext(array $parentContext, $attribute) + protected function createChildContext(array $parentContext, $attribute/*, string $format = null*/) { - $context = parent::createChildContext($parentContext, $attribute); + if (\func_num_args() >= 3) { + $format = \func_get_arg(2); + } else { + @trigger_error(sprintf('Method %s::%s() will have a 3rd `string $format = null` argument in version 5.0. Not defining it is deprecated since Symfony 3.4.', \get_class($this), __FUNCTION__), E_USER_DEPRECATED); + + $format = null; + } + + $context = parent::createChildContext($parentContext, $attribute, $format); // format is already included in the cache_key of the parent. - $context['cache_key'] = $this->getAttributeCacheKey('', $context); + $context['cache_key'] = $this->getCacheKey($format, $context); return $context; } @@ -396,8 +404,9 @@ protected function createChildContext(array $parentContext, $attribute) * * @return bool|string */ - private function getAttributeCacheKey($format, array $context) + private function getCacheKey($format, array $context) { + unset($context['cache_key']); // avoid artifically different keys try { return md5($format.serialize([ 'context' => $context,