square / pjson

JSON <=> PHP8+ objects serialization / deserialization library
Apache License 2.0
109 stars 7 forks source link

Handle nullable object arrays #20

Closed jasonhebert closed 1 year ago

jasonhebert commented 1 year ago

If a JSON payload contains a property that can be array|null the current functionality breaks when attempting to pass a null value to array_map in Json.php line 76.

A null check has been added so that if the value from the JSON payload is null it returns the null value, otherwise it processes the value as normal.

In the example below an error would have previously been thrown: INPUT:

{
    "nullable_schedules": null
}

CLASS:

class Category
{
    use JsonSerialize;

    #[Json('nullable_schedules', type: Schedule::class)]
    protected ?array $nullableSchedules;
}

ERROR: Fatal error: Uncaught TypeError: array_map(): Argument #2 ($array) must be of type array, null given

khepin commented 1 year ago

PR looks good, I need to figure out a way to enable our CLA bot on this repo. I'll get back to you in a bit.

khepin commented 1 year ago

As this is a Square hosted project, individual contributors are required to sign the CLA in order to contribute. Once you have signed, we'll be able to move forward with the PR.

jasonhebert commented 1 year ago

CLA signed and submitted! Thanks...