1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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 https://github.com/rails/rails/issues/43737
This commit is contained in:
Zoran Pesic 2021-11-27 21:54:07 -08:00
parent 2a32c4b679
commit 1de55c3596
No known key found for this signature in database
GPG key ID: 16CA21084A25C5FE
2 changed files with 12 additions and 0 deletions

View file

@ -140,6 +140,8 @@ module ActiveModel
#
# person.errors.merge!(other)
def merge!(other)
return errors if equal?(other)
other.errors.each { |error|
import(error)
}

View file

@ -657,6 +657,16 @@ class ErrorsTest < ActiveModel::TestCase
assert(person.errors.added?(:name, :blank))
end
test "merge does not import errors when merging with self" do
errors = ActiveModel::Errors.new(Person.new)
errors.add(:name, :invalid)
errors_before_merge = errors.dup
errors.merge!(errors)
assert(errors.errors.eql?(errors_before_merge.errors))
end
test "errors are marshalable" do
errors = ActiveModel::Errors.new(Person.new)
errors.add(:name, :invalid)