Do not leak the Errors default proc when calling to_hash or as_json

This commit is contained in:
Jean Boussier 2016-09-27 14:33:16 +02:00
parent 7750b049ee
commit 33fd23e077
2 changed files with 11 additions and 1 deletions

View File

@ -280,7 +280,7 @@ module ActiveModel
messages[attribute] = array.map { |message| full_message(attribute, message) }
end
else
messages.dup
without_default_proc(messages)
end
end

View File

@ -250,6 +250,16 @@ class ErrorsTest < ActiveModel::TestCase
assert_equal({ name: ["cannot be blank"] }, person.errors.to_hash)
end
test "to_hash returns a hash without default proc" do
person = Person.new
assert_nil person.errors.to_hash.default_proc
end
test "as_json returns a hash without default proc" do
person = Person.new
assert_nil person.errors.as_json.default_proc
end
test "full_messages creates a list of error messages with the attribute name included" do
person = Person.new
person.errors.add(:name, "cannot be blank")