Skip to content

Commit 260c384

Browse files
committed
Merge pull request rails#15432 from sgrif/sg-coder-type-casting
Don't change values in `@raw_attributes` during serialization
2 parents 75f75a8 + 9373462 commit 260c384

File tree

4 files changed

+14
-23
lines changed

4 files changed

+14
-23
lines changed

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def attributes
289289

290290
# Placeholder so it can be overriden when needed by serialization
291291
def attributes_for_coder # :nodoc:
292-
attributes
292+
attributes_before_type_cast
293293
end
294294

295295
# Returns an <tt>#inspect</tt>-like string for the value of the

activerecord/lib/active_record/attribute_methods/serialization.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def attributes_for_coder
138138
attrs[name] = if self.class.serialized_attributes.include?(name)
139139
@raw_attributes[name].serialized_value
140140
else
141-
read_attribute(name)
141+
read_attribute_before_type_cast(name)
142142
end
143143
end
144144
end

activerecord/test/cases/store_test.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,6 @@ class StoreTest < ActiveRecord::TestCase
183183
assert_equal({}, @john.params)
184184
end
185185

186-
test "attributes_for_coder should return stored fields already serialized" do
187-
attributes = {
188-
"id" => @john.id,
189-
"name"=> @john.name,
190-
"settings" => "--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\ncolor: black\n",
191-
"preferences" => "--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\nremember_login: true\n",
192-
"json_data" => "{\"height\":\"tall\"}", "json_data_empty"=>"{\"is_a_good_guy\":true}",
193-
"params" => "--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}\n",
194-
"account_id"=> @john.account_id
195-
}
196-
197-
assert_equal attributes, @john.attributes_for_coder
198-
end
199-
200186
test "dump, load and dump again a model" do
201187
dumped = YAML.dump(@john)
202188
loaded = YAML.load(dumped)

activerecord/test/cases/yaml_serialization_test.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ def test_roundtrip_serialized_column
2323
assert_equal({:omg=>:lol}, YAML.load(YAML.dump(topic)).content)
2424
end
2525

26-
def test_encode_with_coder
27-
topic = Topic.first
28-
coder = {}
29-
topic.encode_with coder
30-
assert_equal({'attributes' => topic.attributes}, coder)
31-
end
32-
3326
def test_psych_roundtrip
3427
topic = Topic.first
3528
assert topic
@@ -47,4 +40,16 @@ def test_psych_roundtrip_new_object
4740
def test_active_record_relation_serialization
4841
[Topic.all].to_yaml
4942
end
43+
44+
def test_raw_types_are_not_changed_on_round_trip
45+
topic = Topic.new(parent_id: "123")
46+
assert_equal "123", topic.parent_id_before_type_cast
47+
assert_equal "123", YAML.load(YAML.dump(topic)).parent_id_before_type_cast
48+
end
49+
50+
def test_cast_types_are_not_changed_on_round_trip
51+
topic = Topic.new(parent_id: "123")
52+
assert_equal 123, topic.parent_id
53+
assert_equal 123, YAML.load(YAML.dump(topic)).parent_id
54+
end
5055
end

0 commit comments

Comments
 (0)