Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 43820b7

Browse filesBrowse files
bug #44650 [Serializer] Make document type nodes ignorable (boenner)
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
2 parents ec00687 + 2c75e27 commit 43820b7
Copy full SHA for 43820b7

File tree

2 files changed

+31
-1
lines changed
Filter options

2 files changed

+31
-1
lines changed

‎src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,13 @@ public function decode($data, $format, array $context = [])
143143
$rootNode = null;
144144
$decoderIgnoredNodeTypes = $context[self::DECODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::DECODER_IGNORED_NODE_TYPES];
145145
foreach ($dom->childNodes as $child) {
146+
if (\in_array($child->nodeType, $decoderIgnoredNodeTypes, true)) {
147+
continue;
148+
}
146149
if (\XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
147150
throw new NotEncodableValueException('Document types are not allowed.');
148151
}
149-
if (!$rootNode && !\in_array($child->nodeType, $decoderIgnoredNodeTypes, true)) {
152+
if (!$rootNode) {
150153
$rootNode = $child;
151154
}
152155
}

‎src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,33 @@ public function testDecodeIgnoreComments()
591591
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
592592
}
593593

594+
public function testDecodeIgnoreDocumentType()
595+
{
596+
$source = <<<'XML'
597+
<?xml version="1.0"?>
598+
<!DOCTYPE people>
599+
<people>
600+
<person>
601+
<firstname>Benjamin</firstname>
602+
<lastname>Alexandre</lastname>
603+
</person>
604+
<person>
605+
<firstname>Damien</firstname>
606+
<lastname>Clay</lastname>
607+
</person>
608+
</people>
609+
XML;
610+
$expected = ['person' => [
611+
['firstname' => 'Benjamin', 'lastname' => 'Alexandre'],
612+
['firstname' => 'Damien', 'lastname' => 'Clay'],
613+
]];
614+
$this->assertEquals($expected, $this->encoder->decode(
615+
$source,
616+
'xml',
617+
[XmlEncoder::DECODER_IGNORED_NODE_TYPES => [\XML_DOCUMENT_TYPE_NODE]]
618+
));
619+
}
620+
594621
public function testDecodePreserveComments()
595622
{
596623
$this->doTestDecodePreserveComments();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.