From d29eb793098a871aec02cd4903af0bf5f76ddd49 Mon Sep 17 00:00:00 2001 From: Nicolas Klein Date: Fri, 5 Jul 2019 16:46:00 +0100 Subject: [PATCH 1/2] Makes sure Grape::API behaves as a Rack::App by calling: 'call' on the first mounted instance rather than the base instance (which is never mounted) which will have the environment information as to where it was mounted --- lib/grape/api.rb | 16 ++++++++++++++-- spec/grape/integration/rack_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/grape/api.rb b/lib/grape/api.rb index d4b70ae3e1..4cc7d702af 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -48,9 +48,13 @@ 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: def make_inheritable(api) # When a child API inherits from a parent API. @@ -147,6 +151,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 e7c9f56659..a1e49a6d81 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 From 0efc8c7d2f9634c30729bea0316bbe2a0109b021 Mon Sep 17 00:00:00 2001 From: Nicolas Klein Date: Fri, 5 Jul 2019 16:50:28 +0100 Subject: [PATCH 2/2] Addresses code style issues --- CHANGELOG.md | 1 + lib/grape/api.rb | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf42e32681..c4ea50fdb5 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 4cc7d702af..a830bb834b 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -49,12 +49,13 @@ def override_all_methods! # NOTE: This will only be called on an API directly mounted on RACK def call(*args, &block) instance_for_rack = if never_mounted? - base_instance - else - mounted_instances.first - end + base_instance + else + mounted_instances.first + end instance_for_rack.call(*args, &block) end + # Allows an API to itself be inheritable: def make_inheritable(api) # When a child API inherits from a parent API.