Skip to content

Commit 1de55c3

Browse files
committed
don't import errors if attempting to merge with self
Prior to this change, attempting to merge an `ActiveModel::Error` instance with itself would result in an endless loop where `ActiveModel::NestedError`s would continue to be imported on the instance until interrupted. Though the merging of identical objects is less likely to happen in practice, this method should still be able to handle such a case gracefully. This change ensures that instances attempting to merge on themselves return early rather than hanging indefinitely. Addresses rails#43737
1 parent 2a32c4b commit 1de55c3

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

activemodel/lib/active_model/errors.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ def import(error, override_options = {})
140140
#
141141
# person.errors.merge!(other)
142142
def merge!(other)
143+
return errors if equal?(other)
144+
143145
other.errors.each { |error|
144146
import(error)
145147
}

activemodel/test/cases/errors_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,16 @@ def call
657657
assert(person.errors.added?(:name, :blank))
658658
end
659659

660+
test "merge does not import errors when merging with self" do
661+
errors = ActiveModel::Errors.new(Person.new)
662+
errors.add(:name, :invalid)
663+
errors_before_merge = errors.dup
664+
665+
errors.merge!(errors)
666+
667+
assert(errors.errors.eql?(errors_before_merge.errors))
668+
end
669+
660670
test "errors are marshalable" do
661671
errors = ActiveModel::Errors.new(Person.new)
662672
errors.add(:name, :invalid)

0 commit comments

Comments
 (0)