Skip to content

Commit

Permalink
bug #44650 [Serializer] Make document type nodes ignorable (boenner)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[Serializer] Make document type nodes ignorable

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #44363
| License       | MIT
| Doc PR        | -

As described in #44363: while looping over all nodes, the current node is checked against the list of ignored node types before throwing the `NotEncodableValueException` if it's a document type node.

Commits
-------

2c75e27 Make document type nodes ignorable
  • Loading branch information
nicolas-grekas committed Feb 18, 2022
2 parents ec00687 + 2c75e27 commit 43820b7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,13 @@ public function decode($data, $format, array $context = [])
$rootNode = null;
$decoderIgnoredNodeTypes = $context[self::DECODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::DECODER_IGNORED_NODE_TYPES];
foreach ($dom->childNodes as $child) {
if (\in_array($child->nodeType, $decoderIgnoredNodeTypes, true)) {
continue;
}
if (\XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
throw new NotEncodableValueException('Document types are not allowed.');
}
if (!$rootNode && !\in_array($child->nodeType, $decoderIgnoredNodeTypes, true)) {
if (!$rootNode) {
$rootNode = $child;
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,33 @@ public function testDecodeIgnoreComments()
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
}

public function testDecodeIgnoreDocumentType()
{
$source = <<<'XML'
<?xml version="1.0"?>
<!DOCTYPE people>
<people>
<person>
<firstname>Benjamin</firstname>
<lastname>Alexandre</lastname>
</person>
<person>
<firstname>Damien</firstname>
<lastname>Clay</lastname>
</person>
</people>
XML;
$expected = ['person' => [
['firstname' => 'Benjamin', 'lastname' => 'Alexandre'],
['firstname' => 'Damien', 'lastname' => 'Clay'],
]];
$this->assertEquals($expected, $this->encoder->decode(
$source,
'xml',
[XmlEncoder::DECODER_IGNORED_NODE_TYPES => [\XML_DOCUMENT_TYPE_NODE]]
));
}

public function testDecodePreserveComments()
{
$this->doTestDecodePreserveComments();
Expand Down

0 comments on commit 43820b7

Please sign in to comment.