8000 feature #28156 [Serializer] Fix the XML comments encoding (maidmaid) · symfony/symfony@2d7aedb · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d7aedb

Browse files
committed
feature #28156 [Serializer] Fix the XML comments encoding (maidmaid)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Serializer] Fix the XML comments encoding | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | / | License | MIT | Doc PR | / When we decode a XML comment, we get `['#comment' => ' foo ']`. But when we encode this same content, the result is not the expected one. ```php $encoder->encode(['#comment' => ' foo '], 'xml'); ``` ``` Expected: <response> <!-- foo --> </response> Actual: <response> <item key="#comment"> foo </item> </response> ``` Commits ------- d94a37f Allow to encode xml comments
2 parents 71794f4 + d94a37f commit 2d7aedb

File tree

3 files changed

+24
-0
lines changed
8000

3 files changed

+24
-0
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* `AbstractNormalizer::handleCircularReference` is now final, and receives two optional extra arguments: the format and the context
8+
* added support for XML comment encoding (encoding `['#comment' => ' foo ']` results `<!-- foo -->`)
89

910
4.1.0
1011
-----

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @author John Wards <jwards@whiteoctober.co.uk>
2323
* @author Fabian Vogler <fabian@equivalence.ch>
2424
* @author Kévin Dunglas <dunglas@gmail.com>
25+
* @author Dany Maillard <danymaillard93b@gmail.com>
2526
*/
2627
class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface, SerializerAwareInterface
2728
{
@@ -226,6 +227,13 @@ final protected function appendDocumentFragment(\DOMNode $node, $fragment): bool
226227
return false;
227228
}
228229

230+
final protected function appendComment(\DOMNode $node, string $data): bool
231+
{
232+
$node->appendChild($this->dom->createComment($data));
233+
234+
return true;
235+
}
236+
229237
/**
230238
* Checks the name is a valid xml element name.
231239
*/
@@ -366,6 +374,8 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName =
366374
$parentNode->setAttribute($attributeName, $data);
367375
} elseif ('#' === $key) {
368376
$append = $this->selectNodeType($parentNode, $data);
377+
} elseif ('#comment' === $key) {
378+
$append = $this->appendComment($parentNode, $data);
369379
} elseif (\is_array($data) && false === is_numeric($key)) {
370380
// Is this array fully numeric keys?
371381
if (ctype_digit(implode('', array_keys($data)))) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,19 @@ public function testEncodeXmlWithDateTimeObjectField()
760760
$this->assertEquals($this->createXmlWithDateTimeField(), $actualXml);
761761
}
762762

763+
public function testEncodeComment()
764+
{
765+
$expected = <<<'XML'
766+
<?xml version="1.0"?>
767+
<response><!-- foo --></response>
768+
769+
XML;
770+
771+
$data = array('#comment' => ' foo ');
772+
773+
$this->assertEquals($expected, $this->encoder->encode($data, 'xml'));
774+
}
775+
763776
/**
764777
* @return XmlEncoder
765778
*/
370C

0 commit comments

Comments
 (0)
0