Skip to content

Commit eb57e56

Browse files
committed
Don't cache Class.instance_methods
Fix: #2258 Some gems or app code may define methods on `Class` after `grape` is loaaded but before `override_all_methods!` is called. As such it's better to check `Class.method_defined?` when doing the override.
1 parent 280e5b3 commit eb57e56

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/grape/api.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Grape
88
# should subclass this class in order to build an API.
99
class API
1010
# Class methods that we want to call on the API rather than on the API object
11-
NON_OVERRIDABLE = (Class.new.methods + %i[call call! configuration compile! inherited]).freeze
11+
NON_OVERRIDABLE = %i[call call! configuration compile! inherited].freeze
1212

1313
class Boolean
1414
def self.build(val)
@@ -50,7 +50,7 @@ def initial_setup(base_instance_parent)
5050

5151
# Redefines all methods so that are forwarded to add_setup and be recorded
5252
def override_all_methods!
53-
(base_instance.methods - NON_OVERRIDABLE).each do |method_override|
53+
(base_instance.methods - Class.methods - NON_OVERRIDABLE).each do |method_override|
5454
define_singleton_method(method_override) do |*args, &block|
5555
add_setup(method_override, *args, &block)
5656
end

spec/grape/api_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4226,6 +4226,15 @@ def self.inherited(child_api)
42264226
end
42274227
end
42284228

4229+
it 'does not override methods inherited from Class' do
4230+
Class.define_method(:test_method) {}
4231+
subclass = Class.new(described_class)
4232+
expect(subclass).to_not receive(:add_setup)
4233+
subclass.test_method
4234+
ensure
4235+
Class.remove_method(:test_method)
4236+
end
4237+
42294238
context 'overriding via composition' do
42304239
module Inherited
42314240
def inherited(api)

0 commit comments

Comments
 (0)