Skip to content

Commit 1587f07

Browse files
committed
Call super in API.inherited
`API.make_inheritable` was overriding `.inherited` hook on given instance itself. But it was not calling `super` which is required to bubble up inheritance chain. This commit removes `.make_inheritable` and calls `super` instead. Fixes PR with reproducible spec #2179.
1 parent 87cb005 commit 1587f07

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

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

lib/grape/api.rb

+4-12
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ def new(*args, &block)
2020

2121
# When inherited, will create a list of all instances (times the API was mounted)
2222
# It will listen to the setup required to mount that endpoint, and replicate it on any new instance
23-
def inherited(api, base_instance_parent = Grape::API::Instance)
24-
api.initial_setup(base_instance_parent)
23+
def inherited(api)
24+
super
25+
26+
api.initial_setup(Grape::API == self ? Grape::API::Instance : @base_instance)
2527
api.override_all_methods!
26-
make_inheritable(api)
2728
end
2829

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

71-
# Allows an API to itself be inheritable:
72-
def make_inheritable(api)
73-
# When a child API inherits from a parent API.
74-
def api.inherited(child_api)
75-
# The instances of the child API inherit from the instances of the parent API
76-
Grape::API.inherited(child_api, base_instance)
77-
end
78-
end
79-
8072
# Alleviates problems with autoloading by tring to search for the constant
8173
def const_missing(*args)
8274
if base_instance.const_defined?(*args)

spec/grape/api_spec.rb

+43
Original file line numberDiff line numberDiff line change
@@ -4081,6 +4081,49 @@ def before
40814081
end
40824082
end
40834083

4084+
describe '.inherited' do
4085+
context 'overriding within class' do
4086+
let(:root_api) do
4087+
Class.new(Grape::API) do
4088+
@bar = 'Hello, world'
4089+
4090+
def self.inherited(child_api)
4091+
super
4092+
child_api.instance_variable_set(:@foo, @bar.dup)
4093+
end
4094+
end
4095+
end
4096+
4097+
let(:child_api) { Class.new(root_api) }
4098+
4099+
it 'allows overriding the hook' do
4100+
expect(child_api.instance_variable_get(:@foo)).to eq('Hello, world')
4101+
end
4102+
end
4103+
4104+
context 'overriding via composition' do
4105+
module Inherited
4106+
def inherited(api)
4107+
super
4108+
api.instance_variable_set(:@foo, @bar.dup)
4109+
end
4110+
end
4111+
4112+
let(:root_api) do
4113+
Class.new(Grape::API) do
4114+
@bar = 'Hello, world'
4115+
extend Inherited
4116+
end
4117+
end
4118+
4119+
let(:child_api) { Class.new(root_api) }
4120+
4121+
it 'allows overriding the hook' do
4122+
expect(child_api.instance_variable_get(:@foo)).to eq('Hello, world')
4123+
end
4124+
end
4125+
end
4126+
40844127
describe 'const_missing' do
40854128
subject(:grape_api) { Class.new(Grape::API) }
40864129
let(:mounted) do

0 commit comments

Comments
 (0)