Skip to content

Allow deserialization of nullable arrays#108

Merged
Crell merged 4 commits into
Crell:masterfrom
agustingomes:issues/107
Jun 7, 2026
Merged

Allow deserialization of nullable arrays#108
Crell merged 4 commits into
Crell:masterfrom
agustingomes:issues/107

Conversation

@agustingomes

Copy link
Copy Markdown
Contributor

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 x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the CONTRIBUTING document.
  • My pull request addresses exactly one patch/feature.
  • I have created a branch for this patch/feature.
  • Each individual commit in the pull request is meaningful.
  • I have added tests to cover my changes.

Comment thread src/PropertyHandler/EnumExporter.php Outdated
Comment thread tests/SerdeTestCases.php Outdated
Comment thread src/Attributes/Field.php
@agustingomes agustingomes force-pushed the issues/107 branch 2 times, most recently from ef2d951 to b2358fc Compare April 26, 2026 21:28
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.
Comment thread README.md Outdated
Comment thread src/Attributes/Field.php Outdated
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.
@Crell

Crell commented Jun 7, 2026

Copy link
Copy Markdown
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 Crell merged commit 98cbb33 into Crell:master Jun 7, 2026
6 checks passed
@agustingomes agustingomes deleted the issues/107 branch June 7, 2026 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deserialization fails on nullable arrays

2 participants