Skip to content

Commit cacf82e

Browse files
authored
Merge pull request #1976 from ruby-grape/bugfix/autoload
Be sure classes/modules listed for autoload really exist
2 parents 6d2e332 + 752bf4c commit cacf82e

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

9+
* [#1976](https://github.com/ruby-grape/grape/pull/1976): Ensure classes/modules listed for autoload really exist - [@dnesteryuk](https://github.com/dnesteryuk).
910
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
1011
* [#1968](https://github.com/ruby-grape/grape/pull/1968): Fix args forwarding in Grape::Middleware::Stack#merge_with for ruby 2.7.0 - [@dm1try](https://github.com/dm1try).
1112

lib/grape.rb

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ module Extensions
8484
eager_autoload do
8585
autoload :DeepMergeableHash
8686
autoload :DeepSymbolizeHash
87-
autoload :DeepHashWithIndifferentAccess
8887
autoload :Hash
8988
end
9089
module ActiveSupport

spec/spec_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
require file
1515
end
1616

17+
eager_load!
18+
1719
# The default value for this setting is true in a standard Rails app,
1820
# so it should be set to true here as well to reflect that.
1921
I18n.enforce_available_locales = true

spec/support/eager_load.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
# Grape uses autoload https://api.rubyonrails.org/classes/ActiveSupport/Autoload.html.
4+
# When a class/module get added to the list, ActiveSupport doesn't check whether it really exists.
5+
# This method loads all classes/modules defined via autoload to be sure only existing
6+
# classes/modules were listed.
7+
def eager_load!(scope = Grape)
8+
# get modules
9+
scope.constants.each do |const_name|
10+
const = scope.const_get(const_name)
11+
12+
next unless const.respond_to?(:eager_load!)
13+
14+
const.eager_load!
15+
16+
# check its modules, they might need to be loaded as well.
17+
eager_load!(const)
18+
end
19+
end

0 commit comments

Comments
 (0)