Skip to content

Commit b70032c

Browse files
committed
[Serializer] Fix XML scalar to object denormalization
1 parent 218313e commit b70032c

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 $c
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;
@@ -548,6 +550,34 @@ public function testDenormalizeUntypedStringObject()
548550
$this->assertEquals(new DummyWithStringObject(new DummyString()), $actual);
549551
$this->assertEquals('', $actual->value->value);
550552
}
553+
554+
public function testDenormalizeXmlScalar()
555+
{
556+
$normalizer = new class () extends AbstractObjectNormalizer
557+
{
558+
public function __construct()
559+
{
560+
parent::__construct(null, new MetadataAwareNameConverter(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()))));
561+
}
562+
563+
protected function extractAttributes(object $object, string $format = null, array $context = []): array
564+
{
565+
return [];
566+
}
567+
568+
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
569+
{
570+
return null;
571+
}
572+
573+
protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = [])
574+
{
575+
$object->$attribute = $value;
576+
}
577+
};
578+
579+
$this->assertSame('scalar', $normalizer->denormalize('scalar', XmlScalarDummy::class, 'xml')->value);
580+
}
551581
}
552582

553583
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -671,6 +701,12 @@ class DummyChild
671701
public $bar;
672702
}
673703

704+
class XmlScalarDummy
705+
{
706+
/** @SerializedName("#") */
707+
public $value;
708+
}
709+
674710
class SerializerCollectionDummy implements SerializerInterface, DenormalizerInterface
675711
{
676712
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