Skip to content

Commit d8e20aa

Browse files
bug #51701 [Serializer] Fix parsing XML root node attributes (mtarld)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix parsing XML root node attributes | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #51594 | License | MIT | Doc PR | N/A Add missing parsing of root node attributes Commits ------- 4dc3cef [Serializer] Fix parsing XML root node attributes
2 parents f8e2aa2 + 4dc3cef commit d8e20aa

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)
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