bug #38669 [Serializer] fix decoding float XML attributes starting with 0 (Marcin Kruk)

This PR was squashed before being merged into the 3.4 branch.

Discussion
----------

[Serializer] fix decoding float XML attributes starting with 0

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #38666
| License       | MIT
| Doc PR        | n/a

This is a naive approach to fix #38666, assuming it is something worth fixing.

I checked different cases and it seems to be fixing all of them, but I bet there will be some other edge cases which still won't be covered properly.

Commits
-------

97b4306c30 [Serializer] fix decoding float XML attributes starting with 0
This commit is contained in:
Fabien Potencier 2020-10-23 06:26:46 +02:00
commit 85e8de6137
2 changed files with 13 additions and 3 deletions

View File

@ -304,7 +304,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
$typeCastAttributes = $this->resolveXmlTypeCastAttributes($context);
foreach ($node->attributes as $attr) {
if (!is_numeric($attr->nodeValue) || !$typeCastAttributes || (isset($attr->nodeValue[1]) && '0' === $attr->nodeValue[0])) {
if (!is_numeric($attr->nodeValue) || !$typeCastAttributes || (isset($attr->nodeValue[1]) && '0' === $attr->nodeValue[0] && '.' !== $attr->nodeValue[1])) {
$data['@'.$attr->nodeName] = $attr->nodeValue;
continue;

View File

@ -268,10 +268,10 @@ XML;
{
$source = <<<XML
<?xml version="1.0"?>
<document index="-12.11">Name</document>
<document index="12.11">Name</document>
XML;
$this->assertSame(['@index' => -12.11, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
$this->assertSame(['@index' => 12.11, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
}
public function testDecodeNegativeFloatAttribute()
@ -284,6 +284,16 @@ XML;
$this->assertSame(['@index' => -12.11, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
}
public function testDecodeFloatAttributeWithZeroWholeNumber()
{
$source = <<<XML
<?xml version="1.0"?>
<document index="0.123">Name</document>
XML;
$this->assertSame(['@index' => 0.123, '#' => 'Name'], $this->encoder->decode($source, 'xml'));
}
public function testNoTypeCastAttribute()
{
$source = <<<XML