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 18, 2020
1 parent 18ed28c commit 415630c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
19 changes: 11 additions & 8 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-03-02 11:38:28 +0100 using RuboCop version 0.80.1.
# on 2020-05-18 13:52:42 +0200 using RuboCop version 0.83.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -13,7 +13,7 @@ Layout/ClosingHeredocIndentation:
- 'spec/grape/api_spec.rb'
- 'spec/grape/entity_spec.rb'

# Offense count: 72
# Offense count: 71
# Cop supports --auto-correct.
Layout/EmptyLineAfterGuardClause:
Enabled: false
Expand Down Expand Up @@ -65,7 +65,8 @@ Lint/ToJSON:
Exclude:
- 'spec/grape/middleware/formatter_spec.rb'

# Offense count: 48
# Offense count: 47
# Configuration parameters: IgnoredMethods.
Metrics/AbcSize:
Max: 43

Expand All @@ -80,21 +81,23 @@ Metrics/BlockLength:
Metrics/ClassLength:
Max: 305

# Offense count: 32
# Offense count: 31
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Max: 14

# Offense count: 61
# Offense count: 60
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 33
Max: 32

# Offense count: 12
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 220

# Offense count: 25
# Offense count: 26
# Configuration parameters: IgnoredMethods.
Metrics/PerceivedComplexity:
Max: 14

Expand Down Expand Up @@ -220,7 +223,7 @@ Style/WordArray:
- 'spec/grape/validations/validators/except_values_spec.rb'
- 'spec/grape/validations/validators/values_spec.rb'

# Offense count: 1314
# Offense count: 1336
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [#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).
* [#2054](https://github.com/ruby-grape/grape/pull/2054): Coercing of nested arrays - [@dnesteryuk](https://github.com/dnesteryuk).
* [#2050](https://github.com/ruby-grape/grape/pull/2053): Fix broken multiple mounts - [@Jack12816](https://github.com/Jack12816).
* Your contribution here.

### 1.3.2 (2020/04/12)
Expand Down
9 changes: 5 additions & 4 deletions lib/grape/api/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,12 @@ def add_head_not_allowed_methods_and_options_methods
route_settings[:requirements] = route.requirements
route_settings[:path] = route.origin
route_settings[:methods] ||= []
route_settings[:methods] << route.request_method
if route.request_method == '*' || route_settings[:methods].include?('*')
route_settings[:methods] = Grape::Http::Headers::SUPPORTED_METHODS
else
route_settings[:methods] << route.request_method
end
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?('*')
end
end

Expand Down
50 changes: 50 additions & 0 deletions spec/grape/api/instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,54 @@ def app
expect(an_instance.top_level_setting.parent).to be_nil
end
end

context 'with multiple moutes' do
let(:first) do
Class.new(Grape::API::Instance) do
namespace(:some_namespace) do
route :any, '*path' do
error!('Not found! (1)', 404)
end
end
end
end
let(:second) do
Class.new(Grape::API::Instance) do
namespace(:another_namespace) do
route :any, '*path' do
error!('Not found! (2)', 404)
end
end
end
end
let(:root_api) do
first_instance = first
second_instance = second
Class.new(Grape::API) do
mount first_instance
mount first_instance
mount second_instance
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 415630c

Please sign in to comment.