Skip to content

Commit b14b196

Browse files
myxohdblock
authored andcommitted
Fixes methods defined on included modules after remountable APIs (#1818)
1 parent 87b6243 commit b14b196

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
* Your contribution here.
66
* [#1813](https://github.com/ruby-grape/grape/pull/1813): Add ruby 2.5 support, drop 2.2. Update rails version in travis - [@darren987469](https://github.com/darren987469).
7-
* [#1803](https://github.com/ruby-grape/grape/pull/1803): Adds the ability to re-mount all endpoints in any location - [@myxoh](https://github.com/bschmeck).
7+
* [#1803](https://github.com/ruby-grape/grape/pull/1803): Adds the ability to re-mount all endpoints in any location - [@myxoh](https://github.com/myxoh).
88
* [#1795](https://github.com/ruby-grape/grape/pull/1795): Fix vendor/subtype parsing of an invalid Accept header - [@bschmeck](https://github.com/bschmeck).
99
* [#1791](https://github.com/ruby-grape/grape/pull/1791): Support `summary`, `hidden`, `deprecated`, `is_array`, `nickname`, `produces`, `consumes`, `tags` options in `desc` block - [@darren987469](https://github.com/darren987469).
1010

lib/grape/api.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module Grape
77
class API
88
# Class methods that we want to call on the API rather than on the API object
99
NON_OVERRIDABLE = %I[define_singleton_method instance_variable_set inspect class is_a? ! kind_of?
10-
respond_to? const_defined? const_missing parent parent_name name equal? to_s parents].freeze
10+
respond_to? respond_to_missing? const_defined? const_missing parent
11+
parent_name name equal? to_s parents].freeze
1112

1213
class << self
1314
attr_accessor :base_instance, :instances
@@ -81,6 +82,19 @@ def respond_to?(method, include_private = false)
8182
super(method, include_private) || base_instance.respond_to?(method, include_private)
8283
end
8384

85+
def respond_to_missing?(method, include_private = false)
86+
base_instance.respond_to?(method, include_private)
87+
end
88+
89+
def method_missing(method, *args, &block)
90+
# If there's a missing method, it may be defined on the base_instance instead.
91+
if respond_to_missing?(method)
92+
base_instance.send(method, *args, &block)
93+
else
94+
super
95+
end
96+
end
97+
8498
private
8599

86100
# Adds a new stage to the set up require to get a Grape::API up and running

spec/grape/api_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,6 +3209,43 @@ def static
32093209
expect { a.mount b }.to_not raise_error
32103210
end
32113211
end
3212+
3213+
context 'when including a module' do
3214+
let(:included_module) do
3215+
Module.new do
3216+
def self.included(base)
3217+
base.extend(ClassMethods)
3218+
end
3219+
module ClassMethods
3220+
def my_method
3221+
@test = true
3222+
end
3223+
end
3224+
end
3225+
end
3226+
3227+
it 'should correctly include module in nested mount' do
3228+
module_to_include = included_module
3229+
v1 = Class.new(Grape::API) do
3230+
version :v1, using: :path
3231+
include module_to_include
3232+
my_method
3233+
end
3234+
v2 = Class.new(Grape::API) do
3235+
version :v2, using: :path
3236+
end
3237+
segment_base = Class.new(Grape::API) do
3238+
mount v1
3239+
mount v2
3240+
end
3241+
3242+
Class.new(Grape::API) do
3243+
mount segment_base
3244+
end
3245+
3246+
expect(v1.my_method).to be_truthy
3247+
end
3248+
end
32123249
end
32133250
end
32143251

0 commit comments

Comments
 (0)