Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
[Serializer] fix denormalization of xml-values to objects when no (op…
…tional) attributes are present #32144
  • Loading branch information
mkrauser committed Dec 10, 2019
commit 16c325c415dda7d37187bae3c1d39b1045c79c93
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