Skip to content

Commit

Permalink
[Serializer] Fix XmlEncoder encoding attribute false
Browse files Browse the repository at this point in the history
  • Loading branch information
alamirault committed Jul 24, 2022
1 parent 7b9addc commit 2f4e11f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ private function buildXml(\DOMNode $parentNode, $data, string $format, array $co
if (!\is_scalar($data)) {
$data = $this->serializer->normalize($data, $format, $context);
}
if (\is_bool($data)) {
$data = (int) $data;
}
$parentNode->setAttribute($attributeName, $data);
} elseif ('#' === $key) {
$append = $this->selectNodeType($parentNode, $data, $format, $context);
Expand Down
8 changes: 8 additions & 0 deletions Tests/Encoder/XmlEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ public function testAttributes()
'föo_bär' => 'a',
'Bar' => [1, 2, 3],
'a' => 'b',
'scalars' => [
'@bool-true' => true,
'@bool-false' => false,
'@int' => 3,
'@float' => 3.4,
'@sring' => 'a',
],
];
$expected = '<?xml version="1.0"?>'."\n".
'<response>'.
Expand All @@ -121,6 +128,7 @@ public function testAttributes()
'<Bar>2</Bar>'.
'<Bar>3</Bar>'.
'<a>b</a>'.
'<scalars bool-true="1" bool-false="0" int="3" float="3.4" sring="a"/>'.
'</response>'."\n";
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
}
Expand Down

0 comments on commit 2f4e11f

Please sign in to comment.