From cbd441fa60dda9f26fc0a23ffb8ae591d5aa89c8 Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Tue, 9 Jun 2026 16:01:22 -0700 Subject: [PATCH 1/2] Keep null for nullable scalar fields in weak mode Signed-off-by: Sai Asish Y --- src/Formatter/ArrayBasedDeformatter.php | 12 ++++++++++ tests/ArrayFormatterTest.php | 21 ++++++++++++++++++ .../NonStrict/NonStrictNullableProperties.php | 22 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 tests/NonStrict/NonStrictNullableProperties.php 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 @@ + Date: Tue, 16 Jun 2026 12:31:14 -0700 Subject: [PATCH 2/2] Move weak-mode null test into shared round_trip examples --- tests/ArrayFormatterTest.php | 21 --------------------- tests/SerdeTestCases.php | 4 ++++ 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/tests/ArrayFormatterTest.php b/tests/ArrayFormatterTest.php index aa3b76b..82f7133 100644 --- a/tests/ArrayFormatterTest.php +++ b/tests/ArrayFormatterTest.php @@ -5,7 +5,6 @@ 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; @@ -87,26 +86,6 @@ 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/SerdeTestCases.php b/tests/SerdeTestCases.php index 9d1641f..69c17fe 100644 --- a/tests/SerdeTestCases.php +++ b/tests/SerdeTestCases.php @@ -11,6 +11,7 @@ use Crell\Serde\Attributes\StaticTypeMap; use Crell\Serde\Attributes\TransitiveTypeField; use Crell\Serde\Formatter\SupportsCollecting; +use Crell\Serde\NonStrict\NonStrictNullableProperties; use Crell\Serde\PropertyHandler\Exporter; use Crell\Serde\PropertyHandler\ObjectExporter; use Crell\Serde\PropertyHandler\ObjectImporter; @@ -316,6 +317,9 @@ public static function round_trip_examples(): iterable yield 'empty_values' => [ 'data' => new EmptyData('beep', null), ]; + yield 'non_strict_null_stays_null' => [ + 'data' => new NonStrictNullableProperties(), + ]; yield 'native_object_serialization' => [ 'data' => new NativeSerUn( 1,