Skip to content

[Serializer] Fix issue related to handling namespace attributes in the XmlEncoder class #52401

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 2 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
40 changes: 19 additions & 21 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,34 +261,32 @@ private function parseXml(\DOMNode $node, array $context = []): array|string
/**
* Parse the input DOMNode attributes into an array.
*/
private function parseXmlAttributes(\DOMNode $node, array $context = []): array
{
if (!$node->hasAttributes()) {
return [];
}

$data = [];
$typeCastAttributes = (bool) ($context[self::TYPE_CAST_ATTRIBUTES] ?? $this->defaultContext[self::TYPE_CAST_ATTRIBUTES]);

foreach ($node->attributes as $attr) {
if (!is_numeric($attr->nodeValue) || !$typeCastAttributes || (isset($attr->nodeValue[1]) && '0' === $attr->nodeValue[0] && '.' !== $attr->nodeValue[1])) {
$data['@'.$attr->nodeName] = $attr->nodeValue;
private function parseXmlAttributes(\DOMNode $node, array $context = []): array
{
if (!$node->hasAttributes()) {
return [];
}

continue;
}
$data = [];
$typeCastAttributes = (bool) ($context[self::TYPE_CAST_ATTRIBUTES] ?? $this->defaultContext[self::TYPE_CAST_ATTRIBUTES]);

foreach ($node->attributes as $attr) {
// Check if the attribute has a namespace
$name = $attr->prefix ? $attr->prefix . ':' . $attr->localName : $attr->localName;
if (!is_numeric($attr->nodeValue) || !$typeCastAttributes || (isset($attr->nodeValue[1]) && '0' === $attr->nodeValue[0] && '.' !== $attr->nodeValue[1])) {
$data['@' . $name] = $attr->nodeValue;
} else {
if (false !== $val = filter_var($attr->nodeValue, \FILTER_VALIDATE_INT)) {
$data['@'.$attr->nodeName] = $val;

continue;
$data['@' . $name] = $val;
} else {
$data['@' . $name] = (float) $attr->nodeValue;
}

$data['@'.$attr->nodeName] = (float) $attr->nodeValue;
}

return $data;
}

return $data;
}

/**
* Parse the input DOMNode value (content and children) into an array or a string.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,18 @@ public function testEncodeWithNamespace()

$this->assertEquals($source, $this->encoder->encode($array, 'xml'));
}
public function testParseXmlAttributesWithNamespace()
{
$encoder = new XmlEncoder();

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:ns="http://example.com" ns:attribute="value"></root>';

$data = $encoder->decode($xml, 'xml');

$this->assertEquals(['@xmlns:ns' => 'http://example.com', '@ns:attribute' => 'value'], $data);
}


public function testEncodeSerializerXmlRootNodeNameOption()
{
Expand Down Expand Up @@ -474,6 +486,8 @@ public function testDecodeWithNamespace()
$this->assertEquals($array, $this->encoder->decode($source, 'xml'));
}



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