Skip to content

[Serializer] fix denormalization of xml-values to objects when no xml-attributes are specified #34825

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -329,6 +329,16 @@ public function denormalize($data, $type, $format = null, array $context = [])
$object = $this->instantiateObject($normalizedData, $type, $context, $reflectionClass, $allowedAttributes, $format);
$resolvedClass = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);

if (\is_string($data) && 'xml' === $format && $this->classMetadataFactory->hasMetadataFor($type)) {
$attributes = $this->classMetadataFactory->getMetadataFor($type)->getAttributesMetadata();
foreach ($attributes as $attribute) {
if ('#' === $attribute->getSerializedName()) {
$normalizedData = ['#' => $data];
break;
}
}
}

foreach ($normalizedData as $attribute => $value) {
if ($this->nameConverter) {
$attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
Expand All @@ -24,6 +25,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\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
Expand Down Expand Up @@ -227,6 +229,26 @@ public function hasMetadataFor($value): bool
$this->assertInstanceOf(DummySecondChildQuux::class, $normalizedData->quux);
}

public function testDenormalizeXmlStringNodeWithoutAttributesToObject()
{
$denormalizer = $this->getDenormalizerForStringNode();
// if an xml-node can have children which should be deserialized as string[]
// and only one child exists
$object = $denormalizer->denormalize('string-value', DummyObjectWithOptionalAttributes::class, 'xml');
$this->assertInstanceOf(DummyObjectWithOptionalAttributes::class, $object);
$this->assertEquals('string-value', $object->value);
$this->assertNull($object->foo);
}

public function getDenormalizerForStringNode()
{
$denormalizer = new AbstractObjectNormalizerWithMetadataAndNameConverter();
$serializer = new Serializer([$denormalizer]);
$denormalizer->setSerializer($serializer);

return $denormalizer;
}

/**
* Test that additional attributes throw an exception if no metadata factory is specified.
*/
Expand Down Expand Up @@ -302,6 +324,44 @@ class StringCollection
public $children;
}

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

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

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

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

class DummyObjectWithOptionalAttributes
{
/**
* @SerializedName("#")
*
* @var string
*/
public $value;
/**
* @SerializedName("@foo")
*
* @var string
*/
public $foo = null;
}

class DummyCollection
{
/** @var DummyChild[] */
Expand Down