Skip to content

[Serializer] Cast numeric/null into xml node value to php type instead of string #34901

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 3 commits 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
4 changes: 4 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

5.1.0
-----
* Cast to PHP type numeric value from node for XML if `XmlEncoder::TYPE_CAST_NODES` set to true into context

5.0.0
-----

Expand Down
18 changes: 17 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
const ROOT_NODE_NAME = 'xml_root_node_name';
const STANDALONE = 'xml_standalone';
const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes';
const TYPE_CAST_NODES = 'xml_type_cast_nodes';
const VERSION = 'xml_version';

private $defaultContext = [
Expand All @@ -62,6 +63,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
self::REMOVE_EMPTY_TAGS => false,
self::ROOT_NODE_NAME => 'response',
self::TYPE_CAST_ATTRIBUTES => true,
self::TYPE_CAST_NODES => true,
];

/**
Expand Down Expand Up @@ -321,12 +323,26 @@ private function parseXmlAttributes(\DOMNode $node, array $context = []): array
*/
private function parseXmlValue(\DOMNode $node, array $context = [])
{
$typeCastNodes = (bool) ($context[self::TYPE_CAST_NODES] ?? $this->defaultContext[self::TYPE_CAST_NODES]);

if (!$node->hasChildNodes()) {
if ($typeCastNodes && '' === $node->nodeValue) {
return null;
}

return $node->nodeValue;
}

if (1 === $node->childNodes->length && \in_array($node->firstChild->nodeType, [XML_TEXT_NODE, XML_CDATA_SECTION_NODE])) {
return $node->firstChild->nodeValue;
if (!is_numeric($node->firstChild->nodeValue) || !$typeCastNodes) {
return $node->firstChild->nodeValue;
}

if (false !== $val = filter_var($node->firstChild->nodeValue, FILTER_VALIDATE_INT)) {
return $val;
}

return (float) $node->firstChild->nodeValue;
}

$value = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,11 @@ public function testNoTypeCastAttribute()
</document>
XML;

$data = $this->encoder->decode($source, 'xml', ['xml_type_cast_attributes' => false]);
$data = $this->encoder->decode($source, 'xml', [
XmlEncoder::TYPE_CAST_ATTRIBUTES => false,
XmlEncoder::TYPE_CAST_NODES => false,
]);

$expected = [
'@a' => '018',
'@b' => '-12.11',
Expand Down Expand Up @@ -439,6 +443,36 @@ public function testDecodeScalarRootAttributes()
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
}

public function testDecodeNumericNodeValue()
{
$source = '<?xml version="1.0"?>'."\n".
'<response><foo>7643796</foo><bar>3.14</bar></response>'."\n";

$expected = [
'foo' => 7643796,
'bar' => 3.14,
];

$result = $this->encoder->decode($source, 'xml');
$this->assertEquals($expected, $result);
$this->assertIsInt($result['foo']);
$this->assertIsFloat($result['bar']);
}

public function testDecodeEmptyNodeValue()
{
$source = '<?xml version="1.0"?>'."\n".
'<response><foo/></response>'."\n";

$expected = [
'foo' => null,
];

$result = $this->encoder->decode($source, 'xml');
$this->assertEquals($expected, $result);
$this->assertNull($result['foo']);
}

public function testDecodeRootAttributes()
{
$source = '<?xml version="1.0"?>'."\n".
Expand Down