Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call super in API.inherited #2180

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

* [#2176](https://github.com/ruby-grape/grape/pull/2176): Fix: OPTIONS fails if matching all routes - [@myxoh](https://github.com/myxoh).
* [#2177](https://github.com/ruby-grape/grape/pull/2177): Fix: `default` validator fails if preceded by `as` validator - [@Catsuko](https://github.com/Catsuko).
* [#2180](https://github.com/ruby-grape/grape/pull/2180): Call `super` in `API.inherited` - [@yogeshjain999](https://github.com/yogeshjain999).
* Your contribution here.
### 1.5.3 (2021/03/07)

Expand Down
16 changes: 4 additions & 12 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ def new(*args, &block)

# When inherited, will create a list of all instances (times the API was mounted)
# It will listen to the setup required to mount that endpoint, and replicate it on any new instance
def inherited(api, base_instance_parent = Grape::API::Instance)
api.initial_setup(base_instance_parent)
def inherited(api)
super

api.initial_setup(Grape::API == self ? Grape::API::Instance : @base_instance)
api.override_all_methods!
make_inheritable(api)
end

# Initialize the instance variables on the remountable class, and the base_instance
Expand Down Expand Up @@ -68,15 +69,6 @@ def call(*args, &block)
instance_for_rack.call(*args, &block)
end

# Allows an API to itself be inheritable:
def make_inheritable(api)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? Couldn't we just call super in #inherited and leave the rest as it is? 🍻

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling this method with super was creating warnings about method redefinations as override_all_methods! gets called twice.

I guess make_inheritable was defined in order to pass extra argument, instead of super.

# When a child API inherits from a parent API.
def api.inherited(child_api)
# The instances of the child API inherit from the instances of the parent API
Grape::API.inherited(child_api, base_instance)
end
end

# Alleviates problems with autoloading by tring to search for the constant
def const_missing(*args)
if base_instance.const_defined?(*args)
Expand Down
43 changes: 43 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4081,6 +4081,49 @@ def before
end
end

describe '.inherited' do
context 'overriding within class' do
let(:root_api) do
Class.new(Grape::API) do
@bar = 'Hello, world'

def self.inherited(child_api)
super
child_api.instance_variable_set(:@foo, @bar.dup)
end
end
end

let(:child_api) { Class.new(root_api) }

it 'allows overriding the hook' do
expect(child_api.instance_variable_get(:@foo)).to eq('Hello, world')
end
end

context 'overriding via composition' do
module Inherited
def inherited(api)
super
api.instance_variable_set(:@foo, @bar.dup)
end
end

let(:root_api) do
Class.new(Grape::API) do
@bar = 'Hello, world'
extend Inherited
end
end

let(:child_api) { Class.new(root_api) }

it 'allows overriding the hook' do
expect(child_api.instance_variable_get(:@foo)).to eq('Hello, world')
end
end
end

describe 'const_missing' do
subject(:grape_api) { Class.new(Grape::API) }
let(:mounted) do
Expand Down