Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,17 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
// if a value is meant to be a string, float, int or a boolean value from the serialized representation.
// That's why we have to transform the values, if one of these non-string basic datatypes is expected.
if (\is_string($data) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
if ('' === $data && $type->isNullable() && \in_array($type->getBuiltinType(), [Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true)) {
return null;
if ('' === $data) {
if (Type::BUILTIN_TYPE_ARRAY === $builtinType = $type->getBuiltinType()) {
return [];
}

if ($type->isNullable() && \in_array($builtinType, [Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true)) {
return null;
}
}

switch ($type->getBuiltinType()) {
switch ($builtinType ?? $type->getBuiltinType()) {
case Type::BUILTIN_TYPE_BOOL:
// according to https://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"
if ('false' === $data || '0' === $data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
class ObjectDummy
{
protected $foo;
/**
* @var array
*/
public $bar;
private $baz;
protected $camelCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ public function testDenormalize()
$this->assertTrue($obj->isBaz());
}

public function testDenormalizeEmptyXmlArray()
{
$normalizer = $this->getDenormalizerForObjectToPopulate();
$obj = $normalizer->denormalize(
['bar' => ''],
ObjectDummy::class,
'xml'
);

$this->assertIsArray($obj->bar);
$this->assertEmpty($obj->bar);
}

public function testDenormalizeWithObject()
{
$data = new \stdClass();
Expand Down