Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/Formatter/ArrayBasedDeformatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function deserializeInt(mixed $decoded, Field $field): int|DeformatterRes
}

// Weak mode.
if ($field->nullable && is_null($value)) {
return null;
}
return (int)($decoded[$field->serializedName]);
}

Expand All @@ -60,6 +63,9 @@ public function deserializeFloat(mixed $decoded, Field $field): float|Deformatte
}

// Weak mode.
if ($field->nullable && is_null($value)) {
return null;
}
return (float)($decoded[$field->serializedName]);
}

Expand All @@ -79,6 +85,9 @@ public function deserializeBool(mixed $decoded, Field $field): bool|DeformatterR
}

// Weak mode.
if ($field->nullable && is_null($value)) {
return null;
}
return (bool)($decoded[$field->serializedName]);
}

Expand All @@ -98,6 +107,9 @@ public function deserializeString(mixed $decoded, Field $field): string|Deformat
}

// Weak mode.
if ($field->nullable && is_null($value)) {
return null;
}
return (string)($value);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/ArrayFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Crell\Serde;

use Crell\Serde\Formatter\ArrayFormatter;
use Crell\Serde\NonStrict\NonStrictNullableProperties;
use Crell\Serde\PropertyHandler\EnumOnArrayImporter;
use Crell\Serde\Records\BackedSize;
use Crell\Serde\Records\LiteralEnums;
Expand Down Expand Up @@ -86,6 +87,26 @@ public function literal_enums(): void
self::assertEquals($expected, $result);
}

#[Test]
public function non_strict_null_stays_null_on_nullable_fields(): void

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this test here, rather than in SerdeTestCases? This should be tested on all formatters.

It should just be another case in round_trip_examples(), so it is automatically tested everywhere.

{
$s = new SerdeCommon(formatters: $this->formatters);

$serialized = [
'int' => null,
'float' => null,
'string' => null,
'bool' => null,
];

$result = $s->deserialize($serialized, from: 'array', to: NonStrictNullableProperties::class);

self::assertNull($result->int);
self::assertNull($result->float);
self::assertNull($result->string);
self::assertNull($result->bool);
}

public static function non_strict_properties_examples(): iterable
{
foreach (self::non_strict_properties_examples_data() as $k => $v) {
Expand Down
22 changes: 22 additions & 0 deletions tests/NonStrict/NonStrictNullableProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Crell\Serde\NonStrict;

use Crell\Serde\Attributes\Field;

class NonStrictNullableProperties
{
public function __construct(
#[Field(strict: false)]
public readonly ?int $int = null,
#[Field(strict: false)]
public readonly ?float $float = null,
#[Field(strict: false)]
public readonly ?string $string = null,
#[Field(strict: false)]
public readonly ?bool $bool = null,
) {
}
}
Loading