提交 da8cd519 编写于 作者: A Aaron Patterson 提交者: GitHub

Merge pull request #25670 from tdtran/fix-nil-json

Serialize JSON attribute value nil as SQL NULL, not JSON 'null'
* Serialize JSON attribute value `nil` as SQL `NULL`, not JSON `null`
*Trung Duc Tran*
* Return `true` from `update_attribute` when the value of the attribute
to be updated is unchanged.
......
......@@ -17,7 +17,11 @@ def deserialize(value)
end
def serialize(value)
::ActiveSupport::JSON.encode(value)
if value.nil?
nil
else
::ActiveSupport::JSON.encode(value)
end
end
def accessor
......
......@@ -102,6 +102,22 @@ def test_select_array_json_value
assert_equal(["v0", { "k1" => "v1" }], x.payload)
end
def test_select_nil_json_after_create
json = JsonDataType.create(payload: nil)
x = JsonDataType.where(payload:nil).first
assert_equal(json, x)
end
def test_select_nil_json_after_update
json = JsonDataType.create(payload: "foo")
x = JsonDataType.where(payload:nil).first
assert_equal(nil, x)
json.update_attributes payload: nil
x = JsonDataType.where(payload:nil).first
assert_equal(json.reload, x)
end
def test_rewrite_array_json_value
@connection.execute %q|insert into json_data_type (payload) VALUES ('["v0",{"k1":"v1"}]')|
x = JsonDataType.first
......
......@@ -113,6 +113,22 @@ def test_null_json
assert_equal(nil, x.payload)
end
def test_select_nil_json_after_create
json = JsonDataType.create(payload: nil)
x = JsonDataType.where(payload:nil).first
assert_equal(json, x)
end
def test_select_nil_json_after_update
json = JsonDataType.create(payload: "foo")
x = JsonDataType.where(payload:nil).first
assert_equal(nil, x)
json.update_attributes payload: nil
x = JsonDataType.where(payload:nil).first
assert_equal(json.reload, x)
end
def test_select_array_json_value
@connection.execute %q|insert into json_data_type (payload) VALUES ('["v0",{"k1":"v1"}]')|
x = JsonDataType.first
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册