Skip to content

Commit 2f4e11f

Browse files
committed
[Serializer] Fix XmlEncoder encoding attribute false
1 parent 7b9addc commit 2f4e11f

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

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);

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)