BUG: v1.0.1 issues with escaped json
Closed this issue · 1 comments
Tell us about your environment
Ruby Version:
3.0.4
Rails Version:
6.1.5.1
PostgreSQL Version:
12
Store Attribute Version:
1.0.1
What did you do?
We have a model with a json type (example below).
class TestObject < ActiveRecord::Base
store_accessor :params, form_data: :json
end
What did you expect to happen?
When reading or writing to form_data, we would expect the values within to act like hashes.
What actually happened?
We're getting a lot of json values that are escaped multiple times – as many as three times.
This is causing errors in our code because the values returned are strings rather than (the expected) hashes.
Example value in db (params)
{"form_data": ""{\"user_query\":\"\"}""}
Returned
"{"user_query":""}"
Expected
{"user_query" => ""}
I don't know what could be causing the serialization portion but the deserialization does have something strange (to me) happening. On the line below, "hash[key]" returns an transformed value for form_data "{"user_query":""}" – I would expect the raw value ({"form_data": ""{\"user_query\":\"\"}""}).
This is invalid. When you use store/serialize with a json or jsonb data type in PG, the adaptor casts it and then so does the coder. The Rails docs calls this out – we didn't RTFM.