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

Skip to content

Commit 4dc3cef

Browse files
committed
[Serializer] Fix parsing XML root node attributes
1 parent 7c4f174 commit 4dc3cef

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,7 @@ public function decode(string $data, string $format, array $context = [])
159159
return $rootNode->nodeValue;
160160
}
161161

162-
$data = [];
163-
164-
foreach ($rootNode->attributes as $attrKey => $attr) {
165-
$data['@'.$attrKey] = $attr->nodeValue;
166-
}
167-
168-
$data['#'] = $rootNode->nodeValue;
169-
170-
return $data;
162+
return array_merge($this->parseXmlAttributes($rootNode, $context), ['#' => $rootNode->nodeValue]);
171163
}
172164

173165
/**

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,21 @@ public function testDecodeFloatAttributeWithZeroWholeNumber()
315315
$this->assertSame(['@index' => 0.123, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
316316
}
317317

318+
public function testNoTypeCastRootAttribute()
319+
{
320+
$source = <<<XML
321+
<?xml version="1.0"?>
322+
<document a="123"></document>
323+
XML;
324+
325+
$data = $this->encoder->decode($source, 'xml', ['xml_type_cast_attributes' => false]);
326+
$expected = [
327+
'@a' => '123',
328+
'#' => '',
329+
];
330+
$this->assertSame($expected, $data);
331+
}
332+
318333
public function testNoTypeCastAttribute()
319334
{
320335
$source = <<<XML

0 commit comments

Comments
 (0)