Skip to content

Commit 9a5ea71

Browse files
committed
bug #17984 Allow to normalize \Traversable when serializing xml (Ener-Getick)
This PR was merged into the 2.3 branch. Discussion ---------- Allow to normalize \Traversable when serializing xml | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | no | Fixed tickets | | License | MIT It's impossible to normalize an object implementing ``\Traversable`` when using the ``XMLEncoder``. For example we can't customize the serializer output when serializing a ``FormInterface`` instance. So my proposition is to fix this by using the default XML encoder output only when the serializer can't normalize the data. Commits ------- 97c5d27 Allow to normalize \Traversable
2 parents 8713c7e + 97c5d27 commit 9a5ea71

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ private function buildXml(\DOMElement $parentNode, $data, $xmlRootNodeName = nul
305305
{
306306
$append = true;
307307

308-
if (is_array($data) || $data instanceof \Traversable) {
308+
if (is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) {
309309
foreach ($data as $key => $data) {
310310
//Ah this is the magic @ attribute types.
311311
if (0 === strpos($key, '@') && is_scalar($data) && $this->isElementNameValid($attributeName = substr($key, 1))) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Encoder;
1313

1414
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
15+
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
1516
use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy;
1617
use Symfony\Component\Serializer\Encoder\XmlEncoder;
1718
use Symfony\Component\Serializer\Serializer;
@@ -203,6 +204,21 @@ public function testEncodeSerializerXmlRootNodeNameOption()
203204
$this->assertEquals($expected, $serializer->serialize($array, 'xml', $options));
204205
}
205206

207+
public function testEncodeTraversableWhenNormalizable() {
208+
$this->encoder = new XmlEncoder();
209+
$serializer = new Serializer(array(new CustomNormalizer()), array('xml' => new XmlEncoder()));
210+
$this->encoder->setSerializer($serializer);
211+
212+
$expected = <<<XML
213+
<?xml version="1.0"?>
214+
<response><foo>normalizedFoo</foo><bar>normalizedBar</bar></response>
215+
216+
XML;
217+
218+
$this->assertEquals($expected, $serializer->serialize(new NormalizableTraversableDummy(), 'xml'));
219+
220+
}
221+
206222
public function testDecode()
207223
{
208224
$source = $this->getXmlSource();

0 commit comments

Comments
 (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