Skip to content

Commit f7cb80a

Browse files
committed
fix(mapper): case-insensitive null string check in Integer/Float casters, add factory integration tests for nullable path
1 parent eb16e8f commit f7cb80a

6 files changed

Lines changed: 21 additions & 0 deletions

File tree

packages/mapper/src/Casters/FloatCaster.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public static function configure(PropertyReflector $property, Context $context):
3535

3636
public function cast(mixed $input): ?float
3737
{
38+
if (is_string($input)) {
39+
$input = mb_strtolower($input);
40+
}
41+
3842
if ($this->nullable && ($input === null || $input === '' || $input === 'null')) {
3943
return null;
4044
}

packages/mapper/src/Casters/IntegerCaster.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public static function configure(PropertyReflector $property, Context $context):
3535

3636
public function cast(mixed $input): ?int
3737
{
38+
if (is_string($input)) {
39+
$input = mb_strtolower($input);
40+
}
41+
3842
if ($this->nullable && ($input === null || $input === '' || $input === 'null')) {
3943
return null;
4044
}

tests/Integration/Mapper/CasterFactoryTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ public function test_for_property(): void
2424
$class = reflect(ObjectWithSerializerProperties::class);
2525

2626
$this->assertInstanceOf(IntegerCaster::class, $factory->forProperty($class->getProperty('intProp')));
27+
$this->assertInstanceOf(IntegerCaster::class, $factory->forProperty($class->getProperty('nullableIntProp')));
2728
$this->assertInstanceOf(FloatCaster::class, $factory->forProperty($class->getProperty('floatProp')));
29+
$this->assertInstanceOf(FloatCaster::class, $factory->forProperty($class->getProperty('nullableFloatProp')));
2830
$this->assertInstanceOf(BooleanCaster::class, $factory->forProperty($class->getProperty('boolProp')));
31+
$this->assertInstanceOf(BooleanCaster::class, $factory->forProperty($class->getProperty('nullableBoolProp')));
2932
$this->assertInstanceOf(NativeDateTimeCaster::class, $factory->forProperty($class->getProperty('nativeDateTimeImmutableProp')));
3033
$this->assertInstanceOf(NativeDateTimeCaster::class, $factory->forProperty($class->getProperty('nativeDateTimeProp')));
3134
$this->assertInstanceOf(NativeDateTimeCaster::class, $factory->forProperty($class->getProperty('nativeDateTimeInterfaceProp')));

tests/Integration/Mapper/Casters/FloatCasterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public function nullable_cast_with_values(mixed $input, float $expected): void
4141
#[TestWith([null])]
4242
#[TestWith([''])]
4343
#[TestWith(['null'])]
44+
#[TestWith(['NULL'])]
45+
#[TestWith(['Null'])]
4446
public function nullable_cast_returns_null_for_empty_input(mixed $input): void
4547
{
4648
$caster = new FloatCaster(nullable: true);

tests/Integration/Mapper/Casters/IntegerCasterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public function nullable_cast_with_values(mixed $input, int $expected): void
4141
#[TestWith([null])]
4242
#[TestWith([''])]
4343
#[TestWith(['null'])]
44+
#[TestWith(['NULL'])]
45+
#[TestWith(['Null'])]
4446
public function nullable_cast_returns_null_for_empty_input(mixed $input): void
4547
{
4648
$caster = new IntegerCaster(nullable: true);

tests/Integration/Mapper/Fixtures/ObjectWithSerializerProperties.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ final class ObjectWithSerializerProperties
1818

1919
public int $intProp = 1;
2020

21+
public ?int $nullableIntProp = null;
22+
2123
public float $floatProp = 0.1;
2224

25+
public ?float $nullableFloatProp = null;
26+
2327
public bool $boolProp = true;
2428

29+
public ?bool $nullableBoolProp = null;
30+
2531
public array $arrayProp = ['a'];
2632

2733
#[SerializeWith(DoubleStringSerializer::class)]

0 commit comments

Comments
 (0)