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

Skip to content

Commit 3434775

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

File tree

3 files changed

+67
-54
lines changed

3 files changed

+67
-54
lines changed

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

Lines changed: 47 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,42 @@ 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+
/**
306+
* Gets the discriminator property name by object.
307+
*
308+
* @param object $object
309+
*/
310+
private function getDiscriminatorProperty($object): ?string
311+
{
312+
$cacheKey = \get_class($object);
313+
if (!\array_key_exists($cacheKey, $this->discriminatorCache)) {
314+
$this->discriminatorCache[$cacheKey] = null;
315+
if ($this->classDiscriminatorResolver) {
316+
$mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object);
317+
$this->discriminatorCache[$cacheKey] = null === $mapping ? null : $mapping->getTypeProperty();
318+
}
319+
}
320+
return $this->discriminatorCache[$cacheKey];
321+
}
322+
280323
/**
281324
* Extracts attributes to normalize from the class of the given object, format and context.
282325
*
@@ -350,7 +393,7 @@ public function denormalize($data, $type, $format = null, array $context = [])
350393

351394
if ($context[self::DEEP_OBJECT_TO_POPULATE] ?? $this->defaultContext[self::DEEP_OBJECT_TO_POPULATE] ?? false) {
352395
try {
353-
$context[self::OBJECT_TO_POPULATE] = $this->getAttributeValue($object, $attribute, $format, $context);
396+
$context[self::OBJECT_TO_POPULATE] = $this->getValue($object, $attribute, $format, $context);
354397
} catch (NoSuchPropertyException $e) {
355398
}
356399
}

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)