Skip to content

[Serializer] Fix XML scalar to object denormalization #52698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2024
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 @@ -369,6 +369,10 @@ public function denormalize($data, string $type, ?string $format = null, array $
return null;
}

if (XmlEncoder::FORMAT === $format && !\is_array($data)) {
$data = ['#' => $data];
}

$allowedAttributes = $this->getAllowedAttributes($type, $context, true);
$normalizedData = $this->prepareForDenormalization($data);
$extraAttributes = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
Expand All @@ -30,6 +31,7 @@
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
Expand Down Expand Up @@ -658,6 +660,34 @@ protected function createChildContext(array $parentContext, string $attribute, ?

$this->assertFalse($normalizer->childContextCacheKey);
}

public function testDenormalizeXmlScalar()
{
$normalizer = new class () extends AbstractObjectNormalizer
{
public function __construct()
{
parent::__construct(null, new MetadataAwareNameConverter(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()))));
}

protected function extractAttributes(object $object, string $format = null, array $context = []): array
{
return [];
}

protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
{
return null;
}

protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = [])
{
$object->$attribute = $value;
}
};

$this->assertSame('scalar', $normalizer->denormalize('scalar', XmlScalarDummy::class, 'xml')->value);
}
}

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down Expand Up @@ -781,6 +811,12 @@ class DummyChild
public $bar;
}

class XmlScalarDummy
{
/** @SerializedName("#") */
public $value;
}

class SerializerCollectionDummy implements SerializerInterface, DenormalizerInterface
{
private $normalizers;
Expand Down
Loading