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/Attributes/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,18 @@ public function validate(mixed $value): bool
};
}

/**
* We're returning early under this conditions due to the fact that a SequenceField
* instance call to `validate` with a null value would throw an exception.
*
* Unfortunately at this moment, only the value can be passed to the `validate`,
* meaning we cannot check `$this->nullable`, which would allow us to have this check inside the `validate`
* method of the SequenceField instance.
*/
if ($this->phpType === 'array' && $this->nullable && $value === null && $valid) {
return true;
}

// The value validates if it passes the simple check above,
// plus the typeField check, if any.
return $valid && ($this->typeField?->validate($value) ?? true);
Expand Down
19 changes: 19 additions & 0 deletions tests/Records/NullablePointList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Crell\Serde\Records;

use Crell\Serde\Attributes\SequenceField;

readonly final class NullablePointList
{
/**
* @param list<Point>|null $points
*/
public function __construct(
#[SequenceField(arrayType: Point::class)]
public array|null $points = null,
) {
}
}
24 changes: 24 additions & 0 deletions tests/Records/NullablePointListWithoutConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Crell\Serde\Records;

use Crell\Serde\Attributes\Field;
use Crell\Serde\Attributes\SequenceField;

readonly final class NullablePointListWithoutConstructor
{
/**
* When the object does not have a constructor, We need to specify the default value.
*
* In cases of readonly properties, we can't use the default value
* as the property can't have a default value in that scenario.
*
* Therefore, #[Field(default: null)] must be used in such circumstances
* to avoid the object instantiation with uninitialized properties.
*/
#[Field(default: null)]
#[SequenceField(arrayType: Point::class)]
public array|null $points;
}
13 changes: 13 additions & 0 deletions tests/SerdeTestCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
use Crell\Serde\Records\NestedFlattenObject;
use Crell\Serde\Records\NestedObject;
use Crell\Serde\Records\NonPromotedDefault;
use Crell\Serde\Records\NullablePointListWithoutConstructor;
use Crell\Serde\Records\NullablePointList;
use Crell\Serde\Records\NullArrays;
use Crell\Serde\Records\NullProps;
use Crell\Serde\Records\OptionalPoint;
Expand Down Expand Up @@ -120,6 +122,7 @@
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Crell\Serde\Records\UnionTypes;
use ReflectionClass;

/**
* Testing base class.
Expand Down Expand Up @@ -352,6 +355,16 @@ interfaceProperty: new \DateTimeImmutable('2025-12-25 12:34:56.789'),
extendsProperty: new DateTimeExtends('2025-12-25 12:34:56.789'),
),
];
yield 'nullable_list_with_constructor' => [
'data' => new NullablePointList(),
];

$reflected = new ReflectionClass(NullablePointListWithoutConstructor::class);
$instance = $reflected->newInstanceWithoutConstructor();
$reflected->getProperty('points')->setValue($instance, null);
yield 'nullable_list_without_constructor' => [
'data' => $instance,
];
}

public static function value_object_flatten_examples(): \Generator
Expand Down
Loading