提交 e8460f8b 编写于 作者: S Sean Griffin

Don't error when invalid json is assigned to a JSON column

Keeping with our behavior elsewhere in the system, invalid input is
assumed to be `nil`.

Fixes #18629.
上级 14599a57
* Invalid values assigned to a JSON column are assumed to be `nil`.
Fixes #18629.
*Sean Griffin*
* Add `ActiveRecord::Base#accessed_fields`, which can be used to quickly
discover which fields were read from a model when you are looking to only
select the data you need from the database.
......
......@@ -11,7 +11,7 @@ def type
def type_cast_from_database(value)
if value.is_a?(::String)
::ActiveSupport::JSON.decode(value)
::ActiveSupport::JSON.decode(value) rescue nil
else
super
end
......
......@@ -179,6 +179,14 @@ def test_changes_in_place
assert_equal({ 'one' => 'two', 'three' => 'four' }, json.payload)
assert_not json.changed?
end
def test_assigning_invalid_json
json = JsonDataType.new
json.payload = 'foo'
assert_nil json.payload
end
end
class PostgresqlJSONTest < ActiveRecord::TestCase
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册