Skip to content

Commit

Permalink
Makes sure Grape::API behaves as a Rack::App (ruby-grape#1893)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
myxoh authored and basjanssen committed Feb 28, 2020
1 parent 357b4a0 commit 750159c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 14 additions & 1 deletion lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
22 changes: 22 additions & 0 deletions spec/grape/integration/rack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 750159c

Please sign in to comment.