From 33fd23e0772fab8932b66486e2c4993376db7f94 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Tue, 27 Sep 2016 14:33:16 +0200 Subject: Do not leak the Errors default proc when calling to_hash or as_json --- activemodel/lib/active_model/errors.rb | 2 +- activemodel/test/cases/errors_test.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 191039f598..72746e194e 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -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 diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 3288b5543c..95ca1f3969 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -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") -- cgit v1.2.3