aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2016-05-12 09:06:29 -0400
committerSean Griffin <sean@seantheprogrammer.com>2016-05-12 09:09:47 -0400
commit6007e584d824225e51f47ba0684d48ea3eb8f518 (patch)
treed75d636ca8c927b6c4502a6bd06725a04d0a2ad2 /activerecord/lib
parent548c1d6e8b819ca4e02e6218b67107c580ee65f2 (diff)
downloadrails-6007e584d824225e51f47ba0684d48ea3eb8f518.tar.gz
rails-6007e584d824225e51f47ba0684d48ea3eb8f518.tar.bz2
rails-6007e584d824225e51f47ba0684d48ea3eb8f518.zip
Fix false positive mutation detection when JSON is used with serialize
When looking for mutation, we compare the serialized version of the value to the before_type_cast form. `Type::Serialized` was breaking this contract by passing the already serialized attribute to the subtype's mutation detection. This never manifested previously, as all mutable subtypes either didn't do anything in their `serialize` method, or had a way to detect double serialization (e.g. `is_a?(String)`). However, now that JSON types can handle string primitives, we need to avoid double serialization. Fixes #24993.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/type/serialized.rb8
1 files changed, 7 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/type/serialized.rb b/activerecord/lib/active_record/type/serialized.rb
index 4ff0740cfb..a3a5241780 100644
--- a/activerecord/lib/active_record/type/serialized.rb
+++ b/activerecord/lib/active_record/type/serialized.rb
@@ -32,7 +32,7 @@ module ActiveRecord
def changed_in_place?(raw_old_value, value)
return false if value.nil?
- raw_new_value = serialize(value)
+ raw_new_value = encoded(value)
raw_old_value.nil? != raw_new_value.nil? ||
subtype.changed_in_place?(raw_old_value, raw_new_value)
end
@@ -52,6 +52,12 @@ module ActiveRecord
def default_value?(value)
value == coder.load(nil)
end
+
+ def encoded(value)
+ unless default_value?(value)
+ coder.dump(value)
+ end
+ end
end
end
end