Skip to content

Commit 9633ef2

Browse files
committed
[Serializer] Fix XML scalar to object denormalization
1 parent dcbfcb4 commit 9633ef2

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
@@ -365,6 +365,10 @@ public function denormalize($data, string $type, string $format = null, array $c
365365
return null;
366366
}
367367

368+
if (XmlEncoder::FORMAT === $format && !\is_array($data)) {
369+
$data = ['#' => $data];
370+
}
371+
368372
$allowedAttributes = $this->getAllowedAttributes($type, $context, true);
369373
$normalizedData = $this->prepareForDenormalization($data);
370374
$extraAttributes = [];

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
1919
use Symfony\Component\PropertyInfo\Type;
2020
use Symfony\Component\Serializer\Annotation\Ignore;
21+
use Symfony\Component\Serializer\Annotation\SerializedName;
2122
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
2223
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
2324
use Symfony\Component\Serializer\Exception\LogicException;
@@ -31,6 +32,7 @@
3132
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
3233
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
3334
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
35+
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
3436
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3537
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
3638
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
@@ -516,6 +518,34 @@ public function testDenormalizeUntypedStringObject()
516518
$this->assertEquals(new DummyWithStringObject(new DummyString()), $actual);
517519
$this->assertEquals('', $actual->value->value);
518520
}
521+
522+
public function testDenormalizeXmlScalar()
523+
{
524+
$normalizer = new class () extends AbstractObjectNormalizer
525+
{
526+
public function __construct()
527+
{
528+
parent::__construct(null, new MetadataAwareNameConverter(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()))));
529+
}
530+
531+
protected function extractAttributes(object $object, string $format = null, array $context = []): array
532+
{
533+
return [];
534+
}
535+
536+
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
537+
{
538+
return null;
539+
}
540+
541+
protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = [])
542+
{
543+
$object->$attribute = $value;
544+
}
545+
};
546+
547+
$this->assertSame('scalar', $normalizer->denormalize('scalar', XmlScalarDummy::class, 'xml')->value);
548+
}
519549
}
520550

521551
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -643,6 +673,12 @@ class DummyChild
643673
public $bar;
644674
}
645675

676+
class XmlScalarDummy
677+
{
678+
/** @SerializedName("#") */
679+
public string $value;
680+
}
681+
646682
class SerializerCollectionDummy implements SerializerInterface, DenormalizerInterface
647683
{
648684
private $normalizers;

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