Skip to content

Commit

Permalink
Merge pull request rails#16162 from chancancode/fix_json_coder
Browse files Browse the repository at this point in the history
Fixed JSON coder when loading NULL from DB
  • Loading branch information
chancancode committed Jul 15, 2014
1 parent a66aeb8 commit f4c8a4b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/coders/json.rb
Expand Up @@ -6,7 +6,7 @@ def self.dump(obj)
end

def self.load(json)
ActiveSupport::JSON.decode(json)
ActiveSupport::JSON.decode(json) unless json.nil?
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions activerecord/test/cases/serialized_attribute_test.rb
Expand Up @@ -81,6 +81,24 @@ def test_serialized_json_attribute_returns_unserialized_value
assert_equal(my_post.title, t.content["title"])
end

# This is to ensure that the JSON coder is behaving the same way as 4.0, but
# we can consider changing this in the future.
def test_json_db_null
Topic.serialize :content, JSON

# Force a row to have a database NULL instead of a JSON "null"
id = Topic.connection.insert "INSERT INTO topics (content) VALUES(NULL)"
t = Topic.find(id)

assert_nil t.content

t.save!

# On 4.0, re-saving a row with a database NULL will turn that into a JSON
# "null"
assert_equal 1, Topic.where('content = "null"').count
end

def test_serialized_attribute_declared_in_subclass
hash = { 'important1' => 'value1', 'important2' => 'value2' }
important_topic = ImportantTopic.create("important" => hash)
Expand Down

0 comments on commit f4c8a4b

Please sign in to comment.