Skip to content

Commit 039fd94

Browse files
committed
bug symfony#32438 [Serializer] XmlEncoder: don't cast padded strings (ogizanagi)
This PR was merged into the 3.4 branch. Discussion ---------- [Serializer] XmlEncoder: don't cast padded strings | Q | A | ------------- | --- | Branch? | 3.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | symfony#23122 (comment) <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A This was a suggestion of @nicolas-grekas in symfony#23122. Which seems to have been forgotten. But shouldn't we also avoid casting something like `.18`, `+18`, `-18`? Commits ------- c1bfaa1 [Serializer] XmlEncoder: don't cast padded strings
2 parents 54c77e6 + c1bfaa1 commit 039fd94

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ private function parseXmlAttributes(\DOMNode $node, array $context = [])
303303
$typeCastAttributes = $this->resolveXmlTypeCastAttributes($context);
304304

305305
foreach ($node->attributes as $attr) {
306-
if (!is_numeric($attr->nodeValue) || !$typeCastAttributes) {
306+
if (!is_numeric($attr->nodeValue) || !$typeCastAttributes || (isset($attr->nodeValue[1]) && '0' === $attr->nodeValue[0])) {
307307
$data['@'.$attr->nodeName] = $attr->nodeValue;
308308

309309
continue;

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

+11
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,17 @@ public function testNoTypeCastAttribute()
306306
$this->assertSame($expected, $data);
307307
}
308308

309+
public function testDoesNotTypeCastStringsStartingWith0()
310+
{
311+
$source = <<<XML
312+
<?xml version="1.0"?>
313+
<document a="018"></document>
314+
XML;
315+
316+
$data = $this->encoder->decode($source, 'xml');
317+
$this->assertSame('018', $data['@a']);
318+
}
319+
309320
public function testEncode()
310321
{
311322
$source = $this->getXmlSource();

0 commit comments

Comments
 (0)