Skip to content

Commit

Permalink
Fix infinite loop when mounting endpoint with same superclass
Browse files Browse the repository at this point in the history
Subclass APIs were being assigned the same top level inheritable
settings from the superclass. When an API attempted to mount another
API with the same superclass, the same setting object would be added to
the setting stack. When an attempt was made to retrieve a value from the
settings, it would sit in loop calling itself.
  • Loading branch information
jkowens committed Jan 6, 2018
1 parent 74e21ab commit 6244ff8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Metrics/AbcSize:
# Offense count: 282
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 3117
Max: 3125

# Offense count: 9
# Configuration parameters: CountComments.
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 @@
* [#1724](https://github.com/ruby-grape/grape/pull/1724): Optional nested array validation - [@ericproulx](https://github.com/ericproulx).
* [#1725](https://github.com/ruby-grape/grape/pull/1725): Fix `rescue_from :all` documentation - [@Jelkster](https://github.com/Jelkster).
* [#1726](https://github.com/ruby-grape/grape/pull/1726): Improved startup performance during API method generation - [@jkowens](https://github.com/jkowens).
* [#1727](https://github.com/ruby-grape/grape/pull/1727): Fix infinite loop when mounting endpoint with same superclass - [@jkowens](https://github.com/jkowens).
* Your contribution here.

### 1.0.1 (9/8/2017)
Expand Down
12 changes: 6 additions & 6 deletions lib/grape/dsl/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ def within_namespace(&_block)

private

# Builds the current class :inheritable_setting. If available, it returns the superclass's :inheritable_setting.
# Otherwise, a clean :inheritable_setting is returned.
# Builds the current class :inheritable_setting. If available, it inherits from
# the superclass's :inheritable_setting.
def build_top_level_setting
if defined?(superclass) && superclass.respond_to?(:inheritable_setting) && superclass != Grape::API
superclass.inheritable_setting
else
Grape::Util::InheritableSetting.new
Grape::Util::InheritableSetting.new.tap do |setting|
if defined?(superclass) && superclass.respond_to?(:inheritable_setting) && superclass != Grape::API
setting.inherit_from superclass.inheritable_setting
end
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3129,6 +3129,16 @@ def static
get '/two/v1/world'
expect(last_response.status).to eq 200
end

context 'when mounting class extends a subclass of Grape::API' do
it 'mounts APIs with the same superclass' do
base_api = Class.new(Grape::API)
a = Class.new(base_api)
b = Class.new(base_api)

expect { a.mount b }.to_not raise_error
end
end
end
end

Expand Down

0 comments on commit 6244ff8

Please sign in to comment.