diff --git a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php index 77c98377ee70a..bc7363cf0eae5 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php @@ -30,6 +30,8 @@ class ObjectNormalizer extends AbstractObjectNormalizer { protected $propertyAccessor; + private $discriminatorCache = array(); + public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null) { if (!\class_exists(PropertyAccess::class)) { @@ -110,15 +112,16 @@ protected function extractAttributes($object, $format = null, array $context = a */ protected function getAttributeValue($object, $attribute, $format = null, array $context = array()) { - if (null !== $this->classDiscriminatorResolver) { - $mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object); - - if (null !== $mapping && $attribute == $mapping->getTypeProperty()) { - return $this->classDiscriminatorResolver->getTypeForMappedObject($object); + $cacheKey = \get_class($object); + if (!array_key_exists($cacheKey, $this->discriminatorCache)) { + $this->discriminatorCache[$cacheKey] = null; + if (null !== $this->classDiscriminatorResolver) { + $mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object); + $this->discriminatorCache[$cacheKey] = null === $mapping ? null : $mapping->getTypeProperty(); } } - return $this->propertyAccessor->getValue($object, $attribute); + return $attribute === $this->discriminatorCache[$cacheKey] ? $this->classDiscriminatorResolver->getTypeForMappedObject($object) : $this->propertyAccessor->getValue($object, $attribute); } /**