Skip to content

Commit

Permalink
bug #32438 [Serializer] XmlEncoder: don't cast padded strings (ogizan…
Browse files Browse the repository at this point in the history
…agi)

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 | #23122 (comment)   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

This was a suggestion of @nicolas-grekas in #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
  • Loading branch information
fabpot committed Jul 18, 2019
2 parents 54c77e6 + c1bfaa1 commit 039fd94
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Expand Up @@ -303,7 +303,7 @@ private function parseXmlAttributes(\DOMNode $node, array $context = [])
$typeCastAttributes = $this->resolveXmlTypeCastAttributes($context);

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

continue;
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
Expand Up @@ -306,6 +306,17 @@ public function testNoTypeCastAttribute()
$this->assertSame($expected, $data);
}

public function testDoesNotTypeCastStringsStartingWith0()
{
$source = <<<XML
<?xml version="1.0"?>
<document a="018"></document>
XML;

$data = $this->encoder->decode($source, 'xml');
$this->assertSame('018', $data['@a']);
}

public function testEncode()
{
$source = $this->getXmlSource();
Expand Down

0 comments on commit 039fd94

Please sign in to comment.