Skip to content

Commit a5e2916

Browse files
committed
[Serializer] PropertyNormalizer doesn't support DiscriminatorMap
1 parent 1bc3ee7 commit a5e2916

File tree

3 files changed

+68
-54
lines changed

3 files changed

+68
-54
lines changed

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
9393
private $propertyTypeExtractor;
9494
private $typesCache = [];
9595
private $attributesCache = [];
96+
private $discriminatorCache = [];
9697

9798
/**
9899
* @deprecated since Symfony 4.2
@@ -107,8 +108,14 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
107108
*/
108109
protected $classDiscriminatorResolver;
109110

110-
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
111-
{
111+
public function __construct(
112+
ClassMetadataFactoryInterface $classMetadataFactory = null,
113+
NameConverterInterface $nameConverter = null,
114+
PropertyTypeExtractorInterface $propertyTypeExtractor = null,
115+
ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null,
116+
callable $objectClassResolver = null,
117+
array $defaultContext = []
118+
) {
112119
parent::__construct($classMetadataFactory, $nameConverter, $defaultContext);
113120

114121
if (isset($this->defaultContext[self::MAX_DEPTH_HANDLER]) && !\is_callable($this->defaultContext[self::MAX_DEPTH_HANDLER])) {
@@ -180,7 +187,7 @@ public function normalize($object, $format = null, array $context = [])
180187
continue;
181188
}
182189

183-
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
190+
$attributeValue = $this->getValue($object, $attribute, $format, $context);
184191
if ($maxDepthReached) {
185192
$attributeValue = $maxDepthHandler($attributeValue, $object, $attribute, $format, $context);
186193
}
@@ -277,6 +284,43 @@ protected function getAttributes($object, $format = null, array $context)
277284
return $attributes;
278285
}
279286

287+
/**
288+
* @internal This method is wrapper
289+
* Gets the attribute value.
290+
*
291+
* @param object $object
292+
* @param string $attribute
293+
* @param string|null $format
294+
* @param array $context
295+
*
296+
* @return mixed
297+
*/
298+
private function getValue($object, $attribute, $format = null, array $context = [])
299+
{
300+
return $attribute === $this->getDiscriminatorProperty($object)
301+
? $this->classDiscriminatorResolver->getTypeForMappedObject($object)
302+
: $this->getAttributeValue($object, $attribute, $format, $context);
303+
}
304+
/**
305+
* Gets the discriminator property name by object.
306+
*
307+
* @param object $object
308+
*
309+
* @return string|null
310+
*/
311+
private function getDiscriminatorProperty($object): ?string
312+
{
313+
$cacheKey = \get_class($object);
314+
if (!\array_key_exists($cacheKey, $this->discriminatorCache)) {
315+
$this->discriminatorCache[$cacheKey] = null;
316+
if ($this->classDiscriminatorResolver) {
317+
$mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object);
318+
$this->discriminatorCache[$cacheKey] = $mapping === null ? null : $mapping->getTypeProperty();
319+
}
320+
}
321+
return $this->discriminatorCache[$cacheKey];
322+
}
323+
280324
/**
281325
* Extracts attributes to normalize from the class of the given object, format and context.
282326
*
@@ -350,7 +394,7 @@ public function denormalize($data, $type, $format = null, array $context = [])
350394

351395
if ($context[self::DEEP_OBJECT_TO_POPULATE] ?? $this->defaultContext[self::DEEP_OBJECT_TO_POPULATE] ?? false) {
352396
try {
353-
$context[self::OBJECT_TO_POPULATE] = $this->getAttributeValue($object, $attribute, $format, $context);
397+
$context[self::OBJECT_TO_POPULATE] = $this->getValue($object, $attribute, $format, $context);
354398
} catch (NoSuchPropertyException $e) {
355399
}
356400
}

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,33 @@
2828
*/
2929
class ObjectNormalizer extends AbstractObjectNormalizer
3030
{
31+
/** @var PropertyAccessorInterface */
3132
protected $propertyAccessor;
3233

33-
private $discriminatorCache = [];
34-
34+
/** @var callable|\Closure */
3535
private $objectClassResolver;
3636

37-
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
38-
{
37+
public function __construct(
38+
ClassMetadataFactoryInterface $classMetadataFactory = null,
39+
NameConverterInterface $nameConverter = null,
40+
PropertyAccessorInterface $propertyAccessor = null,
41+
PropertyTypeExtractorInterface $propertyTypeExtractor = null,
42+
ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null,
43+
callable $objectClassResolver = null,
44+
array $defaultContext = []
45+
) {
3946
if (!class_exists(PropertyAccess::class)) {
4047
throw new LogicException('The ObjectNormalizer class requires the "PropertyAccess" component. Install "symfony/property-access" to use it.');
4148
}
4249

43-
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext);
50+
parent::__construct(
51+
$classMetadataFactory,
52+
$nameConverter,
53+
$propertyTypeExtractor,
54+
$classDiscriminatorResolver,
55+
$objectClassResolver,
56+
$defaultContext
57+
);
4458

4559
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
4660

@@ -126,16 +140,7 @@ protected function extractAttributes($object, $format = null, array $context = [
126140
*/
127141
protected function getAttributeValue($object, $attribute, $format = null, array $context = [])
128142
{
129-
$cacheKey = \get_class($object);
130-
if (!\array_key_exists($cacheKey, $this->discriminatorCache)) {
131-
$this->discriminatorCache[$cacheKey] = null;
132-
if (null !== $this->classDiscriminatorResolver) {
133-
$mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object);
134-
$this->discriminatorCache[$cacheKey] = null === $mapping ? null : $mapping->getTypeProperty();
135-
}
136-
}
137-
138-
return $attribute === $this->discriminatorCache[$cacheKey] ? $this->classDiscriminatorResolver->getTypeForMappedObject($object) : $this->propertyAccessor->getValue($object, $attribute);
143+
return $this->propertyAccessor->getValue($object, $attribute);
139144
}
140145

141146
/**

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,6 @@
3030
*/
3131
class PropertyNormalizer extends AbstractObjectNormalizer
3232
{
33-
/**
34-
* {@inheritdoc}
35-
*/
36-
public function supportsNormalization($data, $format = null)
37-
{
38-
return parent::supportsNormalization($data, $format) && $this->supports(\get_class($data));
39-
}
40-
41-
/**
42-
* {@inheritdoc}
43-
*/
44-
public function supportsDenormalization($data, $type, $format = null)
45-
{
46-
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
47-
}
48-
4933
/**
5034
* {@inheritdoc}
5135
*/
@@ -54,25 +38,6 @@ public function hasCacheableSupportsMethod(): bool
5438
return __CLASS__ === static::class;
5539
}
5640

57-
/**
58-
* Checks if the given class has any non-static property.
59-
*/
60-
private function supports(string $class): bool
61-
{
62-
$class = new \ReflectionClass($class);
63-
64-
// We look for at least one non-static property
65-
do {
66-
foreach ($class->getProperties() as $property) {
67-
if (!$property->isStatic()) {
68-
return true;
69-
}
70-
}
71-
} while ($class = $class->getParentClass());
72-
73-
return false;
74-
}
75-
7641
/**
7742
* {@inheritdoc}
7843
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy