Skip to content

Commit

Permalink
bug #52698 [Serializer] Fix XML scalar to object denormalization (mta…
Browse files Browse the repository at this point in the history
…rld)

This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Fix XML scalar to object denormalization

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #32114 #34579
| License       | MIT

Another approach to #34825

Commits
-------

6fe892f [Serializer] Fix XML scalar to object denormalization
  • Loading branch information
fabpot committed Apr 5, 2024
2 parents 22e9fd1 + 6fe892f commit 32e57aa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
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
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

0 comments on commit 32e57aa

Please sign in to comment.