Skip to content

Commit

Permalink
Fix bug with TypeMap default values
Browse files Browse the repository at this point in the history
rails#42773 introduced a regression where
looking up an unregistered type on a TypeMap with a parent (like
[mysql2 TYPE_MAP_WITH_BOOLEAN](https://github.com/rails/rails/blob/88ec15b850790a06bd8dcfe59ca05d865f458c7c/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 <[email protected]>
  • Loading branch information
composerinteralia and chrisbloom7 committed Jul 14, 2021
1 parent aae83f4 commit d30c85c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/type/type_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ def alias_type(key, target_key)
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

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
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/type/type_map_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ def test_parent_fallback
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
Expand Down

0 comments on commit d30c85c

Please sign in to comment.