Skip to content

Commit 32e57aa

Browse files
committed
bug symfony#52698 [Serializer] Fix XML scalar to object denormalization (mtarld)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix XML scalar to object denormalization | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#32114 symfony#34579 | License | MIT Another approach to symfony#34825 Commits ------- 6fe892f [Serializer] Fix XML scalar to object denormalization
2 parents 22e9fd1 + 6fe892f commit 32e57aa

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ public function denormalize($data, string $type, ?string $format = null, array $
369369
return null;
370370
}
371371

372+
if (XmlEncoder::FORMAT === $format && !\is_array($data)) {
373+
$data = ['#' => $data];
374+
}
375+
372376
$allowedAttributes = $this->getAllowedAttributes($type, $context, true);
373377
$normalizedData = $this->prepareForDenormalization($data);
374378
$extraAttributes = [];

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1818
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
1919
use Symfony\Component\PropertyInfo\Type;
20+
use Symfony\Component\Serializer\Annotation\SerializedName;
2021
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
2122
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
2223
use Symfony\Component\Serializer\Exception\LogicException;
@@ -30,6 +31,7 @@
3031
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
3132
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
3233
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
34+
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
3335
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3436
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
3537
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
@@ -658,6 +660,34 @@ protected function createChildContext(array $parentContext, string $attribute, ?
658660

659661
$this->assertFalse($normalizer->childContextCacheKey);
660662
}
663+
664+
public function testDenormalizeXmlScalar()
665+
{
666+
$normalizer = new class () extends AbstractObjectNormalizer
667+
{
668+
public function __construct()
669+
{
670+
parent::__construct(null, new MetadataAwareNameConverter(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()))));
671+
}
672+
673+
protected function extractAttributes(object $object, string $format = null, array $context = []): array
674+
{
675+
return [];
676+
}
677+
678+
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
679+
{
680+
return null;
681+
}
682+
683+
protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = [])
684+
{
685+
$object->$attribute = $value;
686+
}
687+
};
688+
689+
$this->assertSame('scalar', $normalizer->denormalize('scalar', XmlScalarDummy::class, 'xml')->value);
690+
}
661691
}
662692

663693
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -781,6 +811,12 @@ class DummyChild
781811
public $bar;
782812
}
783813

814+
class XmlScalarDummy
815+
{
816+
/** @SerializedName("#") */
817+
public $value;
818+
}
819+
784820
class SerializerCollectionDummy implements SerializerInterface, DenormalizerInterface
785821
{
786822
private $normalizers;

0 commit comments

Comments
 (0)