Fix bug with TypeMap default values

https://github.com/rails/rails/pull/42773 introduced a regression where
looking up an unregistered type on a TypeMap with a parent (like
[mysql2 TYPE_MAP_WITH_BOOLEAN](88ec15b850/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb (L618))
would cause a `LocalJumpError`

This commit fixes the error by forwarding the default block when
fetching from the parent TypeMap.

Co-authored-by: Chris Bloom <chrisbloom7@gmail.com>
This commit is contained in:
Daniel Colson 2021-07-14 13:26:37 -04:00
parent aae83f4a68
commit d30c85cebd
No known key found for this signature in database
GPG Key ID: 88A364BBE77B1353
2 changed files with 9 additions and 2 deletions

View File

@ -40,7 +40,7 @@ module ActiveRecord
end
protected
def perform_fetch(lookup_key)
def perform_fetch(lookup_key, &block)
matching_pair = @mapping.reverse_each.detect do |key, _|
key === lookup_key
end
@ -48,7 +48,7 @@ module ActiveRecord
if matching_pair
matching_pair.last.call(lookup_key)
elsif @parent
@parent.perform_fetch(lookup_key)
@parent.perform_fetch(lookup_key, &block)
else
yield lookup_key
end

View File

@ -143,6 +143,13 @@ module ActiveRecord
assert_equal boolean, mapping.lookup("boolean")
end
def test_parent_fallback_for_default_type
parent = klass.new
mapping = klass.new(parent)
assert_kind_of Value, mapping.lookup(:undefined)
end
private
def klass
TypeMap