Skip to content

Commit

Permalink
Fix broken multiple mounts.
Browse files Browse the repository at this point in the history
Signed-off-by: Hermann Mayer <hermann.mayer92@gmail.com>
  • Loading branch information
Jack12816 committed May 14, 2020
1 parent 23374d6 commit 4502ef0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* [#2049](https://github.com/ruby-grape/grape/pull/2049): Coerce an empty string to nil in case of the bool type - [@dnesteryuk](https://github.com/dnesteryuk).
* [#2043](https://github.com/ruby-grape/grape/pull/2043): Modify declared for nested array and hash - [@kadotami](https://github.com/kadotami).
* [#2040](https://github.com/ruby-grape/grape/pull/2040): Fix a regression with Array of type nil - [@ericproulx](https://github.com/ericproulx).
* [#2050](https://github.com/ruby-grape/grape/pull/2050): Fix broken multiple mounts - [@Jack12816](https://github.com/Jack12816).
* Your contribution here.

### 1.3.2 (2020/04/12)
Expand Down
5 changes: 4 additions & 1 deletion lib/grape/api/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ def cascade?
# with a list of HTTP methods that can be called. Also add a route that
# will return an HTTP 405 response for any HTTP method that the resource
# cannot handle.
#
# rubocop:disable Metrics/AbcSize because of the complex workflow
def add_head_not_allowed_methods_and_options_methods
routes_map = {}

Expand All @@ -209,7 +211,7 @@ def add_head_not_allowed_methods_and_options_methods
route_settings[:endpoint] = route.app

# using the :any shorthand produces [nil] for route methods, substitute all manually
route_settings[:methods] = Grape::Http::Headers::SUPPORTED_METHODS if route_settings[:methods].include?('*')
route_settings[:methods] = Grape::Http::Headers::SUPPORTED_METHODS.dup if route_settings[:methods].include?('*')
end
end

Expand Down Expand Up @@ -239,6 +241,7 @@ def add_head_not_allowed_methods_and_options_methods
end
end
end
# rubocop:enable Metrics/AbcSize

# Generate a route that returns an HTTP 405 response for a user defined
# path on methods not specified
Expand Down
44 changes: 44 additions & 0 deletions spec/grape/api/instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,48 @@ def app
expect(an_instance.top_level_setting.parent).to be_nil
end
end

context 'with multiple moutes' do
let(:root_api) do
first = Class.new(Grape::API::Instance) do
namespace(:some_namespace) do
route :any, '*path' do
error!('Not found! (1)', 404)
end
end
end
second = Class.new(Grape::API::Instance) do
namespace(:another_namespace) do
route :any, '*path' do
error!('Not found! (2)', 404)
end
end
end
Class.new(Grape::API) do
mount first
mount first
mount second
end
end

it 'does not raise a FrozenError on first instance' do
expect { patch '/some_namespace/anything' }.not_to \
raise_error
end

it 'responds the correct body at the first instance' do
patch '/some_namespace/anything'
expect(last_response.body).to eq 'Not found! (1)'
end

it 'does not raise a FrozenError on second instance' do
expect { get '/another_namespace/other' }.not_to \
raise_error
end

it 'responds the correct body at the second instance' do
get '/another_namespace/foobar'
expect(last_response.body).to eq 'Not found! (2)'
end
end
end

0 comments on commit 4502ef0

Please sign in to comment.