Skip to content

Commit 97b4306

Browse files
Marcin Krukfabpot
Marcin Kruk
authored andcommitted
[Serializer] fix decoding float XML attributes starting with 0
1 parent 1eead3f commit 97b4306

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

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

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

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

310310
continue;

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@ public function testDecodeFloatAttribute()
268268
{
269269
$source = <<<XML
270270
<?xml version="1.0"?>
271-
<document index="-12.11">Name</document>
271+
<document index="12.11">Name</document>
272272
XML;
273273

274-
$this->assertSame(['@index' => -12.11, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
274+
$this->assertSame(['@index' => 12.11, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
275275
}
276276

277277
public function testDecodeNegativeFloatAttribute()
@@ -284,6 +284,16 @@ public function testDecodeNegativeFloatAttribute()
284284
$this->assertSame(['@index' => -12.11, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
285285
}
286286

287+
public function testDecodeFloatAttributeWithZeroWholeNumber()
288+
{
289+
$source = <<<XML
290+
<?xml version="1.0"?>
291+
<document index="0.123">Name</document>
292+
XML;
293+
294+
$this->assertSame(['@index' => 0.123, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
295+
}
296+
287297
public function testNoTypeCastAttribute()
288298
{
289299
$source = <<<XML

0 commit comments

Comments
 (0)