Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 52a3832

Browse filesBrowse files
Hanmacfabpot
authored andcommitted
[Serializer] Fix deserializing XML Attributes into string properties
1 parent fd9c5b4 commit 52a3832
Copy full SHA for 52a3832

File tree

3 files changed

+49
-0
lines changed
Filter options

3 files changed

+49
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+24Lines changed: 24 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().'[]';
@@ -748,6 +760,18 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
748760
}
749761
}
750762

763+
if (is_numeric($data) && XmlEncoder::FORMAT === $format) {
764+
// encoder parsed them wrong, so they might need to be transformed back
765+
switch ($typeIdentifier) {
766+
case TypeIdentifier::STRING:
767+
return (string) $data;
768+
case TypeIdentifier::FLOAT:
769+
return (float) $data;
770+
case TypeIdentifier::INT:
771+
return (int) $data;
772+
}
773+
}
774+
751775
if ($collectionValueType) {
752776
try {
753777
$collectionValueBaseType = $collectionValueType;
+11Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+14Lines changed: 14 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,18 @@ 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+
797811
public function testUnionTypeDeserializable()
798812
{
799813
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.