8000 [Serializer] Fix deserializing XML Attributes into string properties · symfony/symfony@8032ef8 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 8032ef8

Browse files
committed
[Serializer] Fix deserializing XML Attributes into string properties
1 parent fd9c5b4 commit 8032ef8

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,18 @@ private function validateAndDenormalizeLegacy(array $types, string $currentClass
512512
}
513513
}
514514

515+
if (is_numeric($data) && XmlEncoder::FORMAT === $format) {
516+
// encoder parsed them wrong, so they might need to be transformed back
517+
switch ($builtinType) {
518+
case LegacyType::BUILTIN_TYPE_STRING:
519+
return (string) $data;
520+
case LegacyType::BUILTIN_TYPE_FLOAT:
521+
return (float) $data;
522+
case LegacyType::BUILTIN_TYPE_INT:
523+
return (int) $data;
524+
}
525+
}
526+
515527
if (null !== $collectionValueType && LegacyType::BUILTIN_TYPE_OBJECT === $collectionValueType->getBuiltinType()) {
516528
$builtinType = LegacyType::BUILTIN_TYPE_OBJECT;
517529
$class = $collectionValueType->getClassName().'[]';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;
4+
5+
use Symfony\Component\Serializer\Attribute\SerializedName;
6+
7+
class SerializedNameAttributeDummy
8+
{
9+
#[SerializedName('@foo')]
10+
public string $foo;
11+
}

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
3434
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
3535
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
36+
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
3637
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3738
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
3839
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
@@ -54,6 +55,7 @@
5455
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummy;
5556
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummyFirstChild;
5657
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummySecondChild;
58+
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\SerializedNameAttributeDummy;
5759
use Symfony\Component\Serializer\Tests\Fixtures\DenormalizableDummy;
5860
use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux;
5961
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface;
@@ -794,6 +796,19 @@ public function testDeserializeNullableIntInXml()
794796
$this->assertNull($obj->value);
795797
}
796798

799+
public function testDeserializeIntAsStringPropertyInXML()
800+
{
801+
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
802+
$nameConverter = new MetadataAwareNameConverter($classMetadataFactory);
803+
$extractor = new PropertyInfoExtractor([], [new ReflectionExtractor()]);
804+
$serializer = new Serializer([new ObjectNormalizer($classMetadataFactory, $nameConverter, null, $extractor)], ['xml' => new XmlEncoder()]);
805+
806+
$obj = $serializer->deserialize('<?xml version="1.0" encoding="UTF-8"?><NameAttributeDummy foo="123" />', SerializedNameAttributeDummy::class, 'xml');
807+
808+
$this->assertSame('123', $obj->foo);
809+
}
810+
811+
797812
public function testUnionTypeDeserializable()
798813
{
799814
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());

0 commit comments

Comments
 (0)
0