Skip to content

Commit 2b070b2

Browse files
committed
[Serializer] PropertyNormalizer doesn't support DiscriminatorMap
1 parent 05fe56b commit 2b070b2

File tree

4 files changed

+41
-59
lines changed

4 files changed

+41
-59
lines changed

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

Lines changed: 38 additions & 2 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
@@ -180,7 +181,7 @@ public function normalize($object, $format = null, array $context = [])
180181
continue;
181182
}
182183

183-
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
184+
$attributeValue = $this->getValue($object, $attribute, $format, $context);
184185
if ($maxDepthReached) {
185186
$attributeValue = $maxDepthHandler($attributeValue, $object, $attribute, $format, $context);
186187
}
@@ -277,6 +278,41 @@ protected function getAttributes($object, $format, array $context)
277278
return $attributes;
278279
}
279280

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

351387
if ($context[self::DEEP_OBJECT_TO_POPULATE] ?? $this->defaultContext[self::DEEP_OBJECT_TO_POPULATE] ?? false) {
352388
try {
353-
$context[self::OBJECT_TO_POPULATE] = $this->getAttributeValue($object, $attribute, $format, $context);
389+
$context[self::OBJECT_TO_POPULATE] = $this->getValue($object, $attribute, $format, $context);
354390
} catch (NoSuchPropertyException $e) {
355391
}
356392
}

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
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

3737
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
@@ -126,16 +126,7 @@ protected function extractAttributes($object, $format = null, array $context = [
126126
*/
127127
protected function getAttributeValue($object, $attribute, $format = null, array $context = [])
128128
{
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);
129+
return $this->propertyAccessor->getValue($object, $attribute);
139130
}
140131

141132
/**

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
*/

src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,6 @@ public function testNoTraversableSupport()
412412
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
413413
}
414414

415-
public function testNoStaticPropertySupport()
416-
{
417-
$this->assertFalse($this->normalizer->supportsNormalization(new StaticPropertyDummy()));
418-
}
419-
420415
public function testInheritedPropertiesSupport()
421416
{
422417
$this->assertTrue($this->normalizer->supportsNormalization(new PropertyChildDummy()));
@@ -519,11 +514,6 @@ public function getBar()
519514
}
520515
}
521516

522-
class StaticPropertyDummy
523-
{
524-
private static $property = 'value';
525-
}
526-
527517
class PropertyParentDummy
528518
{
529519
private $foo = 'bar';

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