diff --git a/src/Formatter/ArrayBasedDeformatter.php b/src/Formatter/ArrayBasedDeformatter.php index 4337d77..f2145f3 100644 --- a/src/Formatter/ArrayBasedDeformatter.php +++ b/src/Formatter/ArrayBasedDeformatter.php @@ -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]); } @@ -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]); } @@ -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]); } @@ -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); } diff --git a/tests/ArrayFormatterTest.php b/tests/ArrayFormatterTest.php index 82f7133..aa3b76b 100644 --- a/tests/ArrayFormatterTest.php +++ b/tests/ArrayFormatterTest.php @@ -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; @@ -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 + { + $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) { diff --git a/tests/NonStrict/NonStrictNullableProperties.php b/tests/NonStrict/NonStrictNullableProperties.php new file mode 100644 index 0000000..03a8a06 --- /dev/null +++ b/tests/NonStrict/NonStrictNullableProperties.php @@ -0,0 +1,22 @@ +