Skip to content

Commit

Permalink
bug #22044 [Serializer] [XML] Ignore Process Instruction (jordscream)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.7 branch.

Discussion
----------

[Serializer] [XML] Ignore Process Instruction

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22005
| License       | MIT
| Doc PR        | N/A

This Pull request ignores Process instruction data in XML for decoding the data.

Commits
-------

0c741f5 [Serializer] [XML] Ignore Process Instruction
  • Loading branch information
fabpot committed Mar 21, 2017
2 parents 65260bc + 0c741f5 commit bca4778
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Expand Up @@ -92,14 +92,16 @@ public function decode($data, $format, array $context = array())
throw new UnexpectedValueException($error->message);
}

$rootNode = null;
foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
throw new UnexpectedValueException('Document types are not allowed.');
}
if (!$rootNode && $child->nodeType !== XML_PI_NODE) {
$rootNode = $child;
}
}

$rootNode = $dom->firstChild;

// todo: throw an exception if the root node name is not correctly configured (bc)

if ($rootNode->hasChildNodes()) {
Expand Down Expand Up @@ -329,6 +331,10 @@ private function parseXmlValue(\DOMNode $node)
$value = array();

foreach ($node->childNodes as $subnode) {
if ($subnode->nodeType === XML_PI_NODE) {
continue;
}

$val = $this->parseXml($subnode);

if ('item' === $subnode->nodeName && isset($val['@key'])) {
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
Expand Up @@ -370,6 +370,44 @@ public function testDecodeArray()
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
}

public function testDecodeXMLWithProcessInstruction()
{
$source = <<<'XML'
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="/xsl/xmlverbatimwrapper.xsl"?>
<?display table-view?>
<?sort alpha-ascending?>
<response>
<foo>foo</foo>
<?textinfo whitespace is allowed ?>
<bar>a</bar>
<bar>b</bar>
<baz>
<key>val</key>
<key2>val</key2>
<item key="A B">bar</item>
<item>
<title>title1</title>
</item>
<?item ignore-title ?>
<item>
<title>title2</title>
</item>
<Barry>
<FooBar id="1">
<Baz>Ed</Baz>
</FooBar>
</Barry>
</baz>
<qux>1</qux>
</response>
<?instruction <value> ?>
XML;
$obj = $this->getObject();

$this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
}

public function testDecodeIgnoreWhiteSpace()
{
$source = <<<'XML'
Expand Down

0 comments on commit bca4778

Please sign in to comment.