Skip to content

Commit 6183d66

Browse files
committed
bug #47048 [Serializer] Fix XmlEncoder encoding attribute false (alamirault)
This PR was merged into the 4.4 branch. Discussion ---------- [Serializer] Fix XmlEncoder encoding attribute false | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | Deprecations? | no | Tickets | Fix #47002 | License | MIT | Doc PR | ```php [ 'foo' => [ '@bar' = true, '@baz' = false, ] ] ``` Before patch: ```xml <foo bar="1" baz=""> ``` After patch: ```xml <foo bar="1" baz="0"> ``` Commits ------- a05c42e [Serializer] Fix XmlEncoder encoding attribute false
2 parents dd947db + a05c42e commit 6183d66

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ private function buildXml(\DOMNode $parentNode, $data, string $format, array $co
417417
if (!\is_scalar($data)) {
418418
$data = $this->serializer->normalize($data, $format, $context);
419419
}
420+
if (\is_bool($data)) {
421+
$data = (int) $data;
422+
}
420423
$parentNode->setAttribute($attributeName, $data);
421424
} elseif ('#' === $key) {
422425
$append = $this->selectNodeType($parentNode, $data, $format, $context);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ public function testAttributes()
111111
'föo_bär' => 'a',
112112
'Bar' => [1, 2, 3],
113113
'a' => 'b',
114+
'scalars' => [
115+
'@bool-true' => true,
116+
'@bool-false' => false,
117+
'@int' => 3,
118+
'@float' => 3.4,
119+
'@sring' => 'a',
120+
],
114121
];
115122
$expected = '<?xml version="1.0"?>'."\n".
116123
'<response>'.
@@ -121,6 +128,7 @@ public function testAttributes()
121128
'<Bar>2</Bar>'.
122129
'<Bar>3</Bar>'.
123130
'<a>b</a>'.
131+
'<scalars bool-true="1" bool-false="0" int="3" float="3.4" sring="a"/>'.
124132
'</response>'."\n";
125133
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
126134
}

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