laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

Fix array casts as string

Open hans-thomas opened this issue 5 months ago • 2 comments

Hello everyone :) As described here #3306, when casting an array attribute as array, it will save as a JSON-encoded string. In this PR, I prevent casting an array to a string.

hans-thomas avatar Jun 29 '25 12:06 hans-thomas

Hi @paulinevos, could you please review my PR?

hans-thomas avatar Jul 23 '25 07:07 hans-thomas

@hans-thomas in general, the changes look good. I am a little bit worried about the BC impact of this change, as any fields that previously used an array cast will have been saved as JSON string and won't be read successfully in future. To mitigate this, we need to do two things:

  • Create a new json cast that people should use instead of array (ignore if something like that already exists in Laravel)
  • Emit a deprecation notice in isJsonCastable if someone is using an array cast. If they are, we should tell them to either use the json cast if they want to store the array as a JSON-encoded string, or to remove the array cast if they want to store it as an array. We can then remove that deprecation notice in the next major version.

With the _id vs. id issue fresh in my mind, I don't want to create another data migration problem for users, so we need to give people the option to retain the old behaviour, no matter how useless it may be in MongoDB.

alcaeus avatar Jul 23 '25 09:07 alcaeus