Skip to content
Merged
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
8 changes: 6 additions & 2 deletions src/Attributes/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,19 @@ public function exclude(): bool
*/
public function validate(mixed $value): bool
{
if ($this->nullable && $value === null) {
// If the field is nullable and the value is null, we know it's valid.
// No need to call the validate() method further down.
return true;
}

$valueType = \get_debug_type($value);

if ($this->phpType === $valueType) {
$valid = true;
} elseif ($this->phpType === 'mixed') {
// From a type perspective, mixed accepts anything.
$valid = true;
} elseif ($this->nullable && $valueType === 'null') {
$valid = true;
} elseif (is_object($value) || class_exists($this->phpType) || interface_exists($this->phpType)) {
// For objects, do a type check and we're done.
$valid = $value instanceof $this->phpType;
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