cjmellor/approval

Approving data attribute of type 'array' gets written back with added slashes

gerb-ster opened this issue · 6 comments

In my model I have an attribute which is casts to array:

protected $casts = [ 'applied_person_data' => 'array' ];

After approving a change, the json object which will be written back to the model has added slashes, which breaks the models behavior.

@gerb-ster I can't recall but was this related to the #46 ?

No, this is something else I'm afraid.

It has to do with this line:

['new_data', '=', json_encode($model->getDirtyAttributes())],

The function json_encode is called to json encode the dirty attributes. But if one of those attributes is of the type 'array' it gets double json encoded.

After approval the new_data is converted back to an array on this line:

$model->forceFill($builder->getModel()->new_data->toArray());

and that way you will end up with a json string instead of an array.

I'll open an bugfix-branche and add a test to expose this issue.

Hmm,

I had a quick glance as I had some time. I thought it might be to do with the fact that I am casting new_data and original_data in the Approval model already, as well as encoding it elsewhere, so thought that might be why it is double encoding, but that doesn't seem to be the case as the test still fails 🤔

I can't seem to spot a solution just yet, so if you're able to help out further that'd be great as it does seem like something that could be an issue later, I just haven't had the time lately to focus on Open Source stuff.

Thanks!

@cjmellor Sure, I'll have a look later today!

Closed by #47