Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 93b7530

Browse filesBrowse files
committed
bug #22478 [Serializer] XmlEncoder: fix negative int and large numbers handling (dunglas)
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] XmlEncoder: fix negative int and large numbers handling | Q | A | ------------- | --- | Branch? | 2.7 <!-- see comment below --> | Bug fix? | yes | New feature? | no <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | #22329, #22333 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | n/a Alternative to #22333. * Negative integers are now handled * Float are now handled * Large numbers are converted to float (as the `JsonEncoder` and native PHP functions like `ceil` do) @vlastv, I've adapted your test. Can you check if it fixes your problem? Commits ------- 1eeadb0 [Serializer] XmlEncoder: fix negative int and large numbers handling
2 parents bc6128b + 1eeadb0 commit 93b7530
Copy full SHA for 93b7530

File tree

Expand file treeCollapse file tree

2 files changed

+51
-3
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+51
-3
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
+11-3Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,19 @@ private function parseXmlAttributes(\DOMNode $node)
301301
$data = array();
302302

303303
foreach ($node->attributes as $attr) {
304-
if (ctype_digit($attr->nodeValue)) {
305-
$data['@'.$attr->nodeName] = (int) $attr->nodeValue;
306-
} else {
304+
if (!is_numeric($attr->nodeValue)) {
307305
$data['@'.$attr->nodeName] = $attr->nodeValue;
306+
307+
continue;
308+
}
309+
310+
if (false !== $val = filter_var($attr->nodeValue, FILTER_VALIDATE_INT)) {
311+
$data['@'.$attr->nodeName] = $val;
312+
313+
continue;
308314
}
315+
316+
$data['@'.$attr->nodeName] = (float) $attr->nodeValue;
309317
}
310318

311319
return $data;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,46 @@ public function testDecodeScalar()
222222
$this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
223223
}
224224

225+
public function testDecodeBigDigitAttributes()
226+
{
227+
$source = <<<XML
228+
<?xml version="1.0"?>
229+
<document index="182077241760011681341821060401202210011000045913000000017100">Name</document>
230+
XML;
231+
232+
$this->assertSame(array('@index' => 182077241760011681341821060401202210011000045913000000017100, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
233+
}
234+
235+
public function testDecodeNegativeIntAttribute()
236+
{
237+
$source = <<<XML
238+
<?xml version="1.0"?>
239+
<document index="-1234">Name</document>
240+
XML;
241+
242+
$this->assertSame(array('@index' => -1234, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
243+
}
244+
245+
public function testDecodeFloatAttribute()
246+
{
247+
$source = <<<XML
248+
<?xml version="1.0"?>
249+
<document index="-12.11">Name</document>
250+
XML;
251+
252+
$this->assertSame(array('@index' => -12.11, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
253+
}
254+
255+
public function testDecodeNegativeFloatAttribute()
256+
{
257+
$source = <<<XML
258+
<?xml version="1.0"?>
259+
<document index="-12.11">Name</document>
260+
XML;
261+
262+
$this->assertSame(array('@index' => -12.11, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
263+
}
264+
225265
public function testEncode()
226266
{
227267
$source = $this->getXmlSource();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.