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
BUG: using :json type results in double-encoded value #42
Comments
This has come up recently because we were upgrading from Rails 6.1.7.6 to Rails 7.0. The former will return the values as JSON but the latter does not - it returns a string which was causing errors in usage. |
This can be avoided when using class User < ApplicationRecord
# works fine
store_accessor :params, :form_data
# double encodes
store_accessor :params, form_data: :json
end However, we use |
And that's correct; we have no assumptions regarding the store format (it could be not only JSON but, for example, YAML), we serialize the attribute value into a JSON and it's a string. If you want to store an array within a store field, you shouldn't do any type casting and just set a default: store_attribute :params, :form_data, default: [] I've added the following change to your gist and all tests pass: class User < ApplicationRecord
+ store_attribute :preferences, :skip_provisioning, ActiveModel::Type::Value.new, default: []
- store_attribute :preferences, :skip_provisioning, :json, default: []
store_attribute :preferences, :skip_provisioning_fixed, :json_proxy, default: []
+ store_accessor :preferences, form_data: ActiveModel::Type::Value.new
- store_accessor :preferences, form_data: :json
store_accessor :preferences, form_data_fixed: :json_proxy
store_accessor :preferences, :typeless_form_data
end Note that I had to use Let me know if that's what you was looking for. |
Ruby Version:
3.2.2
Rails Version:
6.1.7.6
PostgreSQL Version:
14
Store Attribute Version:
0.8.1
confirmed on 1.2.0 as well
What did you do?
Specified the type as
:json
when usingstore_attribute
&store_accessor
on a JSONB type PostgreSQL column.What did you expect to happen?
Attributes should serialize as JSON.
What actually happened?
Attributes serialize as a string.
Details
This is a circle back to #28. At the time, I thought the issue constrained to only
store_accessor
and that we simply should not have been specifying a type. But, it seems reasonable that the gem should do the right thing and not re-encode the attribute when using:json
as the type. This affectsstore_attribute
too.Per the ActiveRecord docs
As a workaround, I've created a new type that simply passes through the values on serialize/deserialize. I'm hoping the gem can be updated so we can avoid this.
Reproduction
The text was updated successfully, but these errors were encountered: