@@ -30,6 +30,8 @@ class ObjectNormalizer extends AbstractObjectNormalizer
30
30
{
31
31
protected $ propertyAccessor ;
32
32
33
+ private $ discriminatorCache = array ();
34
+
33
35
public function __construct (ClassMetadataFactoryInterface $ classMetadataFactory = null , NameConverterInterface $ nameConverter = null , PropertyAccessorInterface $ propertyAccessor = null , PropertyTypeExtractorInterface $ propertyTypeExtractor = null , ClassDiscriminatorResolverInterface $ classDiscriminatorResolver = null )
34
36
{
35
37
if (!\class_exists (PropertyAccess::class)) {
@@ -110,15 +112,16 @@ protected function extractAttributes($object, $format = null, array $context = a
110
112
*/
111
113
protected function getAttributeValue ($ object , $ attribute , $ format = null , array $ context = array ())
112
114
{
113
- if (null !== $ this ->classDiscriminatorResolver ) {
114
- $ mapping = $ this ->classDiscriminatorResolver ->getMappingForMappedObject ($ object );
115
-
116
- if (null !== $ mapping && $ attribute == $ mapping ->getTypeProperty ()) {
117
- return $ this ->classDiscriminatorResolver ->getTypeForMappedObject ($ object );
115
+ $ cacheKey = \get_class ($ object );
116
+ if (!array_key_exists ($ cacheKey , $ this ->discriminatorCache )) {
117
+ $ this ->discriminatorCache [$ cacheKey ] = null ;
118
+ if (null !== $ this ->classDiscriminatorResolver ) {
119
+ $ mapping = $ this ->classDiscriminatorResolver ->getMappingForMappedObject ($ object );
120
+ $ this ->discriminatorCache [$ cacheKey ] = null === $ mapping ? null : $ mapping ->getTypeProperty ();
118
121
}
119
122
}
120
123
121
- return $ this ->propertyAccessor ->getValue ($ object , $ attribute );
124
+ return $ attribute === $ this -> discriminatorCache [ $ cacheKey ] ? $ this -> classDiscriminatorResolver -> getTypeForMappedObject ( $ object ) : $ this ->propertyAccessor ->getValue ($ object , $ attribute );
122
125
}
123
126
124
127
/**
0 commit comments