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

JSON columns with array cast are double encoded #43

Closed
hagabaka opened this issue Nov 8, 2018 · 6 comments
Closed

JSON columns with array cast are double encoded #43

hagabaka opened this issue Nov 8, 2018 · 6 comments

Comments

@hagabaka
Copy link
Contributor

hagabaka commented Nov 8, 2018

When a model has a "JSON array castable field", for example by defining protected $casts = ['field' => 'array'], the stored versions will be double JSON encoded. For example, if the original model's column value was {"a": "b"}, the version's getModel() will have the column value "{\"a\": \"b\"}".

This happens because when saving a version, VersionableTrait::versionablePostSave() uses getAttributes(), and when restoring a version, Version::getModel() uses fill($attributes). fill will do conversions based on $casts (JSON encoding in this case), but getAttributes returns attributes as-is (already JSON-encoded string in this case). To fix this, VersionableTrait::versionablePostSave() should use attributesToArray() instead, which would return array castable columns as arrays, so that fill() works expectedly.

@PKuebler
Copy link

unfortunately we just got the bug. can we merge this PR?

thanks :)

@nonoesp
Copy link
Collaborator

nonoesp commented Aug 12, 2020

Hi @hagabaka & @PKuebler - Will take a look!

@PKuebler
Copy link

@nonoesp , @mpociot any updates?

@nonoesp
Copy link
Collaborator

nonoesp commented Aug 31, 2020

@PKuebler - Hi! For your use case, have you checked that #46 fixes the issue?

@PKuebler
Copy link

Yes, I changed the line locally in the vendor and it worked.

@nonoesp nonoesp closed this as completed in 5d0aa71 Sep 1, 2020
@nonoesp
Copy link
Collaborator

nonoesp commented Sep 1, 2020

Just merged this pull request.

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

No branches or pull requests

3 participants