From 928eb2d61faeb5833641f6a4f13c06157d91f527 Mon Sep 17 00:00:00 2001 From: James Sansbury Date: Wed, 7 Mar 2018 08:51:08 -0500 Subject: [PATCH 1/9] [Serializer] Add test to ensure comments don't break XML decoding --- .../Tests/Encoder/XmlEncoderTest.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php index 17f7b93b4d837..d450d7f47efc5 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php @@ -515,6 +515,33 @@ public function testDecodeIgnoreWhiteSpace() $this->assertEquals($expected, $this->encoder->decode($source, 'xml')); } + public function testDecodeIgnoreComments() + { + $source = <<<'XML' + + + + + + Benjamin + Alexandre + + + Damien + Clay + + +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 testDecodeAlwaysAsCollection() { $this->encoder = new XmlEncoder('response', null); From 01ae25fe3f8d6a59abce39c27fb0e42a7dc36252 Mon Sep 17 00:00:00 2001 From: James Sansbury Date: Wed, 7 Mar 2018 09:02:35 -0500 Subject: [PATCH 2/9] [Serializer] Test that decoding XML comments is possible --- .../Tests/Encoder/XmlEncoderTest.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php index d450d7f47efc5..39bb557c3c6c0 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php @@ -542,6 +542,35 @@ public function testDecodeIgnoreComments() $this->assertEquals($expected, $this->encoder->decode($source, 'xml')); } + public function testDecodePreserveComments() + { + $source = <<<'XML' + + + + + Benjamin + Alexandre + + + Damien + Clay + + +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); From 744eea50014d48e02a5390a5f7438056c9e38e4b Mon Sep 17 00:00:00 2001 From: James Sansbury Date: Wed, 7 Mar 2018 09:03:15 -0500 Subject: [PATCH 3/9] [Serializer] Ignore comments when decoding XML Previously, if the first line of XML was a comment, that would be used as the root node of the decoded XML. This work strips comments from decoded XML by default, but also allows for customizing which XML node types are ignored during decoding. --- src/Symfony/Component/Serializer/Encoder/XmlEncoder.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index fec0fefd08ec0..4fc5e1c0a8c57 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -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 array $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) { $this->rootNodeName = $rootNodeName; $this->loadOptions = null !== $loadOptions ? $loadOptions : LIBXML_NONET | LIBXML_NOBLANKS; + $this->ignoredNodeTypes = !empty($ignoredNodeTypes) ? $ignoredNodeTypes : array(XML_PI_NODE, XML_COMMENT_NODE); } /** @@ -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; } } @@ -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; } From 5fb630d94bc163846dc07163f1360f0a05c0354c Mon Sep 17 00:00:00 2001 From: James Sansbury Date: Wed, 7 Mar 2018 09:34:37 -0500 Subject: [PATCH 4/9] [Serializer] Update CHANGELOG.md re: new XmlEncoder argument. --- src/Symfony/Component/Serializer/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index a4e8bf499f5c6..0c0950c81c267 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -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 ----- From 3856734ddf326a49bae04884d988766b39cc1b9b Mon Sep 17 00:00:00 2001 From: James Sansbury Date: Wed, 7 Mar 2018 09:46:13 -0500 Subject: [PATCH 5/9] [Serializer] Fix style issues and @param type. --- src/Symfony/Component/Serializer/Encoder/XmlEncoder.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index 4fc5e1c0a8c57..53f8c4aab64f1 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -42,8 +42,8 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa /** * Construct new XmlEncoder and allow to change the root node element name. * - * @param int|null $loadOptions A bit field of LIBXML_* constants - * @param array $ignoredNodeTypes An array of ignored XML node types, each one of the DOM Predefined XML_* 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, array $ignoredNodeTypes = null) { @@ -108,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 && !\in_array($child->nodeType, $this->ignoredNodeTypes, TRUE)) { + if (!$rootNode && !\in_array($child->nodeType, $this->ignoredNodeTypes, true)) { $rootNode = $child; } } @@ -319,7 +319,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array()) $value = array(); foreach ($node->childNodes as $subnode) { - if (\in_array($subnode->nodeType, $this->ignoredNodeTypes, TRUE)) { + if (\in_array($subnode->nodeType, $this->ignoredNodeTypes, true)) { continue; } From b55454e5b52c64ab182f04b9b87cef297d9cadb5 Mon Sep 17 00:00:00 2001 From: James Sansbury Date: Wed, 7 Mar 2018 10:07:40 -0500 Subject: [PATCH 6/9] [Serializer] Add XmlEncoder:: defaults directly to argument, per @ostrolucky. --- src/Symfony/Component/Serializer/Encoder/XmlEncoder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index 53f8c4aab64f1..0a6cc0edbdcc2 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -45,11 +45,11 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa * @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, array $ignoredNodeTypes = null) + public function __construct(string $rootNodeName = 'response', int $loadOptions = null, array $ignoredNodeTypes = array(XML_PI_NODE, XML_COMMENT_NODE)) { $this->rootNodeName = $rootNodeName; $this->loadOptions = null !== $loadOptions ? $loadOptions : LIBXML_NONET | LIBXML_NOBLANKS; - $this->ignoredNodeTypes = !empty($ignoredNodeTypes) ? $ignoredNodeTypes : array(XML_PI_NODE, XML_COMMENT_NODE); + $this->ignoredNodeTypes = $ignoredNodeTypes; } /** From 16f268332653328a268d7974d433d9eadebce847 Mon Sep 17 00:00:00 2001 From: James Sansbury Date: Thu, 8 Mar 2018 09:46:13 -0500 Subject: [PATCH 7/9] [Serializer] Update documentation to match previous change to argument. --- src/Symfony/Component/Serializer/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 0c0950c81c267..c6f63ead700f5 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -11,8 +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. +* added optional `int[] $ignoredNodeTypes` argument to `XmlEncoder::__construct`. Xml decoding now + ignores comment node types by default. 4.0.0 ----- From 91ec9529b79f0cd676ad947eac8ca686837aba79 Mon Sep 17 00:00:00 2001 From: James Sansbury Date: Mon, 19 Mar 2018 12:41:32 -0400 Subject: [PATCH 8/9] [Serializer] Update the 4.1 upgrade instructions to note XML decoding change. --- UPGRADE-4.1.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/UPGRADE-4.1.md b/UPGRADE-4.1.md index d76e3a3796188..e197d7bcc0066 100644 --- a/UPGRADE-4.1.md +++ b/UPGRADE-4.1.md @@ -74,6 +74,11 @@ SecurityBundle * The `SecurityUserValueResolver` class is deprecated, use `Symfony\Component\Security\Http\Controller\UserValueResolver` instead. +Serializer +---------- + + * Decoding XML with `XmlEncoder` now ignores comment node types by default. + Translation ----------- From 070629bf939ebb61c69dbb242cace004dcd1c9c7 Mon Sep 17 00:00:00 2001 From: James Sansbury Date: Wed, 21 Mar 2018 10:08:39 -0400 Subject: [PATCH 9/9] [Serializer] Use XML instead of Xml in CHANGELOG. --- src/Symfony/Component/Serializer/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index c6f63ead700f5..d45e771e610b8 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -11,7 +11,7 @@ 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 `int[] $ignoredNodeTypes` argument to `XmlEncoder::__construct`. Xml decoding now +* added optional `int[] $ignoredNodeTypes` argument to `XmlEncoder::__construct`. XML decoding now ignores comment node types by default. 4.0.0 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy