Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,18 @@ private function validateAndDenormalizeLegacy(array $types, string $currentClass
}
}

if (is_numeric($data) && XmlEncoder::FORMAT === $format) {
// encoder parsed them wrong, so they might need to be transformed back
switch ($builtinType) {
case LegacyType::BUILTIN_TYPE_STRING:
return (string) $data;
case LegacyType::BUILTIN_TYPE_FLOAT:
return (float) $data;
case LegacyType::BUILTIN_TYPE_INT:
return (int) $data;
}
}

if (null !== $collectionValueType && LegacyType::BUILTIN_TYPE_OBJECT === $collectionValueType->getBuiltinType()) {
$builtinType = LegacyType::BUILTIN_TYPE_OBJECT;
$class = $collectionValueType->getClassName().'[]';
Expand Down Expand Up @@ -748,6 +760,18 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
}
}

if (is_numeric($data) && XmlEncoder::FORMAT === $format) {
// encoder parsed them wrong, so they might need to be transformed back
switch ($typeIdentifier) {
case TypeIdentifier::STRING:
return (string) $data;
case TypeIdentifier::FLOAT:
return (float) $data;
case TypeIdentifier::INT:
return (int) $data;
}
}

if ($collectionValueType) {
try {
$collectionValueBaseType = $collectionValueType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;

use Symfony\Component\Serializer\Attribute\SerializedName;

class SerializedNameAttributeDummy
{
#[SerializedName('@foo')]
public string $foo;
}
14 changes: 14 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
Expand All @@ -54,6 +55,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummyFirstChild;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummySecondChild;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\SerializedNameAttributeDummy;
use Symfony\Component\Serializer\Tests\Fixtures\DenormalizableDummy;
use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface;
Expand Down Expand Up @@ -794,6 +796,18 @@ public function testDeserializeNullableIntInXml()
$this->assertNull($obj->value);
}

public function testDeserializeIntAsStringPropertyInXML()
{
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$nameConverter = new MetadataAwareNameConverter($classMetadataFactory);
$extractor = new PropertyInfoExtractor([], [new ReflectionExtractor()]);
$serializer = new Serializer([new ObjectNormalizer($classMetadataFactory, $nameConverter, null, $extractor)], ['xml' => new XmlEncoder()]);

$obj = $serializer->deserialize('<?xml version="1.0" encoding="UTF-8"?><NameAttributeDummy foo="123" />', SerializedNameAttributeDummy::class, 'xml');

$this->assertSame('123', $obj->foo);
}

public function testUnionTypeDeserializable()
{
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
Expand Down
Loading