8000 [Serializer] Ignore comments when decoding XML by q0rban · Pull Request #26445 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Ignore comments when decoding XML #26445

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

Closed
2 changes: 2 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ CHANGELOG
* added optional `bool $escapeFormulas = false` argument to `CsvEncoder::__construct`
* added `AbstractObjectNormalizer::setMaxDepthHandler` to set a handler to call when the configured
maximum depth is reached
* added optional `array $ignoredNodeTypes = null` argument to `XmlEncoder::__construct`. Xml
decoding now ignores comment node types by default.

4.0.0
-----
Expand Down
11 changes: 7 additions & 4 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
private $context;
private $rootNodeName = 'response';
private $loadOptions;
private $ignoredNodeTypes;

/**
* Construct new XmlEncoder and allow to change the root node element name.
*
* @param int|null $loadOptions A bit field of LIBXML_* constants
* @param int|null $loadOptions A bit field of LIBXML_* constants
* @param int[] $ignoredNodeTypes an array of ignored XML node types, each one of the DOM Predefined XML_* Constants
*/
public function __construct(string $rootNodeName = 'response', int $loadOptions = null)
public function __construct(string $rootNodeName = 'response', int $loadOptions = null, array $ignoredNodeTypes = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

, array $ignoredNodeTypes = array(XML_PI_NODE, XML_COMMENT_NODE)

then you can get rid of ternary

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered about that. I was following the pattern I saw for $loadOptions, but I'm happy to switch to that.

{
$this->rootNodeName = $rootNodeName;
$this->loadOptions = null !== $loadOptions ? $loadOptions : LIBXML_NONET | LIBXML_NOBLANKS;
$this->ignoredNodeTypes = !empty($ignoredNodeTypes) ? $ignoredNodeTypes : array(XML_PI_NODE, XML_COMMENT_NODE);
}

/**
Expand Down Expand Up @@ -105,7 +108,7 @@ public function decode($data, $format, array $context = array())
if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
throw new NotEncodableValueException('Document types are not allowed.');
}
if (!$rootNode && XML_PI_NODE !== $child->nodeType) {
if (!$rootNode && !\in_array($child->nodeType, $this->ignoredNodeTypes, true)) {
$rootNode = $child;
}
}
Expand Down Expand Up @@ -316,7 +319,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array())
$value = array();

foreach ($node->childNodes as $subnode) {
if (XML_PI_NODE === $subnode->nodeType) {
if (\in_array($subnode->nodeType, $this->ignoredNodeTypes, true)) {
continue;
}

Expand Down
56 changes: 56 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,62 @@ public function testDecodeIgnoreWhiteSpace()
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
}

public function testDecodeIgnoreComments()
{
$source = <<<'XML'
<?xml version="1.0"?>
<!-- This comment should not become the root node. -->
<people>
<person>
<!-- Even if the first comment didn't become the root node, we don't
want this comment either. -->
<firstname>Benjamin</firstname>
<lastname>Alexandre</lastname>
</person>
<person>
<firstname>Damien</firstname>
<lastname>Clay</lastname>
</person>
</people>
XML;

$expected = array('person' => array(
array('firstname' => 'Benjamin', 'lastname' => 'Alexandre'),
array('firstname' => 'Damien', 'lastname' => 'Clay'),
));

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

public function testDecodePreserveComments()
{
$source = <<<'XML'
<?xml version="1.0"?>
<people>
<person>
<!-- This comment should be decoded. -->
<firstname>Benjamin</firstname>
<lastname>Alexandre</lastname>
</person>
<person>
<firstname>Damien</firstname>
<lastname>Clay</lastname>
</person>
</people>
XML;

$this->encoder = new XmlEncoder('people', null, array(XML_PI_NODE));
$serializer = new Serializer(array(new CustomNormalizer()), array('xml' => new XmlEncoder()));
$this->encoder->setSerializer($serializer);

$expected = array('person' => array(
array('firstname' => 'Benjamin', 'lastname' => 'Alexandre', '#comment' => ' This comment should be decoded. '),
array('firstname' => 'Damien', 'lastname' => 'Clay'),
));

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

public function testDecodeAlwaysAsCollection()
{
$this->encoder = new XmlEncoder('response', null);
Expand Down
0