From f2ff7372157a5c3c904ec13d7eaef834cdd8c0e1 Mon Sep 17 00:00:00 2001 From: Dmitriy Nesteryuk Date: Wed, 15 Jan 2020 09:30:39 +0200 Subject: [PATCH] Be sure classes/modules listed for autoload really exist --- CHANGELOG.md | 1 + lib/grape.rb | 1 - spec/spec_helper.rb | 20 ++++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13188bb37..045cd2c7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ #### Fixes +* [#](https://github.com/ruby-grape/grape/pull/): Be sure classes/modules listed for autoload really exist - [@dnesteryuk](https://github.com/dnesteryuk). * [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart). * [#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). diff --git a/lib/grape.rb b/lib/grape.rb index 388fb54bd..e5fcc9802 100644 --- a/lib/grape.rb +++ b/lib/grape.rb @@ -84,7 +84,6 @@ module Extensions eager_autoload do autoload :DeepMergeableHash autoload :DeepSymbolizeHash - autoload :DeepHashWithIndifferentAccess autoload :Hash end module ActiveSupport diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fa4f8bf00..e606972cb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,26 @@ require 'bundler' Bundler.require :default, :test +# Grape uses autoload https://api.rubyonrails.org/classes/ActiveSupport/Autoload.html. +# When a class/module get added to the list, ActiveSupport doesn't check whether it really exists. +# This method loads all classes/modules defined via autoload to be sure only existing +# classes/modules were listed. +def force_autoload(scope) + # get modules + scope.constants.each do |const_name| + const = scope.const_get(const_name) + + next unless const.respond_to?(:eager_load!) + + const.eager_load! + + # check its modules, they might need to be loaded as well. + force_autoload(const) + end +end + +force_autoload Grape + Dir["#{File.dirname(__FILE__)}/support/*.rb"].each do |file| require file end