Skip to content

[Serializer] [XML] Ignore Process Instruction #22044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,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