Skip to content

Commit 7a5785d

Browse files
committed
[Serializer] Fix XML attributes not added on empty
1 parent 4be9706 commit 7a5785d

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,22 @@ public function decode(string $data, string $format, array $context = [])
140140
// todo: throw an exception if the root node name is not correctly configured (bc)
141141

142142
if ($rootNode->hasChildNodes()) {
143-
$xpath = new \DOMXPath($dom);
144-
$data = [];
145-
foreach ($xpath->query('namespace::*', $dom->documentElement) as $nsNode) {
146-
$data['@'.$nsNode->nodeName] = $nsNode->nodeValue;
143+
$data = $this->parseXml($rootNode, $context);
144+
if (\is_array($data)) {
145+
$data = $this->addXmlNamespaces($data, $rootNode, $dom);
147146
}
148147

149-
unset($data['@xmlns:xml']);
150-
151-
if (empty($data)) {
152-
return $this->parseXml($rootNode, $context);
153-
}
154-
155-
return array_merge($data, (array) $this->parseXml($rootNode, $context));
148+
return $data;
156149
}
157150

158151
if (!$rootNode->hasAttributes()) {
159152
return $rootNode->nodeValue;
160153
}
161154

162-
return array_merge($this->parseXmlAttributes($rootNode, $context), ['#' => $rootNode->nodeValue]);
155+
$data = array_merge($this->parseXmlAttributes($rootNode, $context), ['#' => $rootNode->nodeValue]);
156+
$data = $this->addXmlNamespaces($data, $rootNode, $dom);
157+
158+
return $data;
163159
}
164160

165161
/**
@@ -344,6 +340,19 @@ private function parseXmlValue(\DOMNode $node, array $context = [])
344340
return $value;
345341
}
346342

343+
private function addXmlNamespaces(array $data, \DOMNode $node, \DOMDocument $document): array
344+
{
345+
$xpath = new \DOMXPath($document);
346+
347+
foreach ($xpath->query('namespace::*', $node) as $nsNode) {
348+
$data['@'.$nsNode->nodeName] = $nsNode->nodeValue;
349+
}
350+
351+
unset($data['@xmlns:xml']);
352+
353+
return $data;
354+
}
355+
347356
/**
348357
* Parse the data and convert it to DOMElements.
349358
*

src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,11 @@ public function testDecodeWithNamespace()
450450
$array = $this->getNamespacedArray();
451451

452452
$this->assertEquals($array, $this->encoder->decode($source, 'xml'));
453+
454+
$source = $this->getEmptyNamespacedXmlSource();
455+
$array = $this->getEmptyNamespacedArray();
456+
457+
$this->assertEquals($array, $this->encoder->decode($source, 'xml'));
453458
}
454459

455460
public function testDecodeScalarWithAttribute()
@@ -797,6 +802,23 @@ protected function getNamespacedArray()
797802
];
798803
}
799804

805+
protected function getEmptyNamespacedXmlSource()
806+
{
807+
return '<?xml version="1.0"?>'."\n".
808+
'<response xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" app:foo="bar">'.
809+
'</response>'."\n";
810+
}
811+
812+
protected function getEmptyNamespacedArray()
813+
{
814+
return [
815+
'@xmlns' => 'http://www.w3.org/2005/Atom',
816+
'@xmlns:app' => 'http://www.w3.org/2007/app',
817+
'@app:foo' => 'bar',
818+
'#' => '',
819+
];
820+
}
821+
800822
protected function getObject()
801823
{
802824
$obj = new Dummy();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy