Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Fix support of nullable type for AsArrayObject/AsCollection #41797

Merged
merged 4 commits into from
Apr 4, 2022

Conversation

a-bashtannik
Copy link
Contributor

Previous fix #36526 of these 2 classes not covered the behavior of php json_encode / json_decode functions when casting null.

// Bugly fragment
public function get($model, $key, $value, $attributes)
{
    // if $attributes[$key] === "null" it is assumed as "set" but ArrayObject can't accept null values.
    return isset($attributes[$key]) ? new ArrayObject(json_decode($attributes[$key], true)) : null;
}

If you have tried to set AsArrayObject attribute value to null (or had it by default), json_encode processed it as sting "null" which is correct from the point of JSON, and it was stored successfully. Nothing wrong here.

But next time you tried to access the attribute, the error ArrayObject::__construct(): Argument #1 ($array) must be of type array, null given was raised because it was already converted to a string and json_decode returns null taking "null" as an argument, which is not acceptable by ArrayObject. Collection class casts null to an empty array under the hood, there was no problem on practice, but I consider better to keep the same predictable behavior of get methods for both cast attribute classes.

This PR also fix potential issues with json_encode / json_decode casting boolean values.

@taylorotwell
Copy link
Member

Why would it return an empty array object vs. null? It should just return null.

@taylorotwell taylorotwell marked this pull request as draft April 2, 2022 15:45
@a-bashtannik
Copy link
Contributor Author

@taylorotwell it was a doubt, but I concluded it should be an empty ArrayObject because otherwise, it ruins the main purpose, and idea of AsArrayObject — casting the attribute to the object and allowing it to behave like an array.

You can't just call

$model->array_object['meta']['title'] = 'Developer';

Because array_object attribute is null.

You need to create something new

$model->array_object = ['meta' => ['title' => 'Developer']];

instead, but that's a bit against AsArrayObject idea, no?

Anyway, both variants are acceptable and close the bug.

@foremtehan
Copy link
Contributor

#38570

@a-bashtannik
Copy link
Contributor Author

@foremtehan you are right, docs are misleading, I followed them in the wrong direction designing the behavior of AsArrayObject/AsCollection.

Ok then, I will update this PR and docs. Should contain also one-two examples more.

$user = new User();
$user->options = ['foo' => ['key' => $value]];
$user->options['foo']['key'] = $value;

@a-bashtannik a-bashtannik marked this pull request as ready for review April 4, 2022 06:40
@taylorotwell taylorotwell merged commit 156773c into laravel:9.x Apr 4, 2022
@AyubM
Copy link

AyubM commented Feb 17, 2023

Taylor, why should it return null instead of an empty ArrayObject? The whole point of casting a property as such is to allow subproperty modification.

As things are, $model->info['foo'] = 'bar' will fail unless the value had previously been set. Which, for new objects, isn't going to be the case.

Major inconvenience if it can return null -- devs either need to check and instantiate as an empty array before modifying any subproperties, or they will have to set the json column to default as {} in the migration as a hack. I spoke too soon, MySQL doesn't accept default values for JSON types. I posted as solution at #38570.

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.

4 participants