Skip to content

Commit a968f3b

Browse files
authored
Merge pull request #60 from openeuropa/GH-59
GH-59: Fix ObjectEncoder dropping attributes with falsy values
2 parents acaca46 + 6f75afc commit a968f3b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Encoder/ObjectEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static function (string $normalizePropertyName, Property $property) use ($object
103103
$iso = $objectAccess->isos[$normalizePropertyName];
104104

105105
return match(true) {
106-
$isAttribute => $value ? (new AttributeBuilder(
106+
$isAttribute => $value !== null ? (new AttributeBuilder(
107107
$type,
108108
$iso->to($value)
109109
))(...) : $defaultAction,

tests/Unit/Encoder/ObjectEncoderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ public static function provideIsomorphicCases(): iterable
109109
'xml' => '<user active="true"><hat><color>green</color></hat></user>',
110110
'data' => new User(active: true, hat: new Hat('green')),
111111
];
112+
yield 'with-falsy-attribute' => [
113+
...$baseConfig,
114+
'context' => self::createContext($xsdType, self::buildTypes(activeAsAttribute: true)),
115+
'xml' => '<user active="false"><hat><color>green</color></hat></user>',
116+
'data' => (object)[
117+
'active' => false,
118+
'hat' => (object)[
119+
'color' => 'green',
120+
],
121+
],
122+
];
123+
yield 'class-objects-with-falsy-attribute' => [
124+
'encoder' => new ObjectEncoder(User::class),
125+
'context' => $withClassMap(self::createContext($xsdType, self::buildTypes(activeAsAttribute: true))),
126+
'xml' => '<user active="false"><hat><color>green</color></hat></user>',
127+
'data' => new User(active: false, hat: new Hat('green')),
128+
];
112129

113130
yield 'wsdl-example-objects' => [
114131
...$baseConfig,

0 commit comments

Comments
 (0)