diff --git a/CHANGELOG.md b/CHANGELOG.md index bf42e3268..c4ea50fdb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ #### Fixes * Your contribution here. +* [#1893](https://github.com/ruby-grape/grape/pull/1893): Allows `Grape::API` to behave like a Rack::app in some instances where it was misbehaving - [@myxoh](https://github.com/myxoh). ### 1.2.4 (2019/06/13) diff --git a/lib/grape/api.rb b/lib/grape/api.rb index d4b70ae3e..a830bb834 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -48,7 +48,12 @@ def override_all_methods! # (http://www.rubydoc.info/github/rack/rack/master/file/SPEC) for more. # NOTE: This will only be called on an API directly mounted on RACK def call(*args, &block) - base_instance.call(*args, &block) + instance_for_rack = if never_mounted? + base_instance + else + mounted_instances.first + end + instance_for_rack.call(*args, &block) end # Allows an API to itself be inheritable: @@ -147,6 +152,14 @@ def evaluate_arguments(configuration, *args) end end end + + def never_mounted? + mounted_instances.empty? + end + + def mounted_instances + instances - [base_instance] + end end end end diff --git a/spec/grape/integration/rack_spec.rb b/spec/grape/integration/rack_spec.rb index e7c9f5665..a1e49a6d8 100644 --- a/spec/grape/integration/rack_spec.rb +++ b/spec/grape/integration/rack_spec.rb @@ -31,4 +31,26 @@ input.unlink end end + + context 'when the app is mounted' do + def app + @main_app ||= Class.new(Grape::API) do + get 'ping' + end + end + + let!(:base) do + app_to_mount = app + Class.new(Grape::API) do + namespace 'namespace' do + mount app_to_mount + end + end + end + + it 'finds the app on the namespace' do + get '/namespace/ping' + expect(last_response.status).to eq 200 + end + end end