Allow deserialization of nullable arrays#108
Merged
Merged
Conversation
b5253e6 to
cb8a519
Compare
Crell
reviewed
Apr 22, 2026
Crell
reviewed
Apr 22, 2026
Crell
reviewed
Apr 22, 2026
ef2d951 to
b2358fc
Compare
The following minimal reproduction case currently ends in failure:
```php
<?php
declare(strict_types=1);
use Crell\Serde\Attributes\SequenceField;
use Crell\Serde\SerdeCommon;
require_once dirname(__DIR__) . '/vendor/autoload.php';
$crellSerde = new SerdeCommon();
$nullableArray = <<<'JSON'
{
"data": null
}
JSON;
final class ListItem {
}
final class MyList {
#[SequenceField(arrayType: ListItem::class)]
public readonly array|null $data;
}
$crellSerde->deserialize(
serialized: $nullableArray,
from: 'json',
to: MyList::class,
);
```
This test addition aims to confirm the bug, and serves as automated test to fix the implementation to achieve the desired behavior.
ef2d951 to
74608f0
Compare
Crell
reviewed
Apr 29, 2026
Crell
reviewed
Apr 29, 2026
This was a case encountered when dealing with an external system as part of a project I was working on.
With this fix, the following minimal reproduction case can work:
```php
<?php
declare(strict_types=1);
use Crell\Serde\Attributes\SequenceField;
use Crell\Serde\SerdeCommon;
require_once dirname(__DIR__) . '/vendor/autoload.php';
$crellSerde = new SerdeCommon();
$nullableArray = <<<'JSON'
{
"data": null
}
JSON;
final class ListItem {
}
final class MyList {
#[SequenceField(arrayType: ListItem::class)]
public readonly array|null $data;
}
$crellSerde->deserialize(
serialized: $nullableArray,
from: 'json',
to: MyList::class,
);
```
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.
Owner
|
Actually, no, I'm dumb. If a field is nullable, and the value is null, then it's guaranteed valid. It doesn't matter if it's an array or sequence or anything else. null is valid in nullable, end of story. So we can simplify the check like this (just pushed), which avoids the need for any array special casing. |
Crell
approved these changes
Jun 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Allows deserialization of nullable arrays
Motivation and context
This was a case encountered when dealing with an external system as part of a project I was working on. With this change, the library can now deserialize nullable arrays.
closes #107
How has this been tested?
Automated test based on the reproduction example linked to the issue were added covering the round trip of serializing/deserializing a nullable array.
Types of changes
What types of changes does your code introduce? Put an
xin all the boxes that apply:Checklist: