Skip to content

Commit

Permalink
Don't cache Class.instance_methods
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
byroot committed May 16, 2023
1 parent 280e5b3 commit 2f4a462
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Grape
# should subclass this class in order to build an API.
class API
# Class methods that we want to call on the API rather than on the API object
NON_OVERRIDABLE = (Class.new.methods + %i[call call! configuration compile! inherited]).freeze
NON_OVERRIDABLE = %i[call call! configuration compile! inherited].freeze

class Boolean
def self.build(val)
Expand Down Expand Up @@ -51,6 +51,8 @@ def initial_setup(base_instance_parent)
# Redefines all methods so that are forwarded to add_setup and be recorded
def override_all_methods!
(base_instance.methods - NON_OVERRIDABLE).each do |method_override|
next if Class.method_defined?(method_override)

define_singleton_method(method_override) do |*args, &block|
add_setup(method_override, *args, &block)
end
Expand Down

0 comments on commit 2f4a462

Please sign in to comment.