Skip to content

Commit 416a7e1

Browse files
committed
Fix retaining setup blocks when remounting APIs
1 parent d40d697 commit 416a7e1

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [#2097](https://github.com/ruby-grape/grape/pull/2097): Skip to set default value unless `meets_dependency?` - [@wanabe](https://github.com/wanabe).
1616
* [#2096](https://github.com/ruby-grape/grape/pull/2096): Fix redundant dependency check - [@braktar](https://github.com/braktar).
1717
* [#2096](https://github.com/ruby-grape/grape/pull/2098): Fix nested coercion - [@braktar](https://github.com/braktar).
18+
* [#2102](https://github.com/ruby-grape/grape/pull/2102): Fix retaining setup blocks when remounting APIs - [@jylamont](https://github.com/jylamont).
1819

1920
### 1.4.0 (2020/07/10)
2021

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ end
1717
group :development do
1818
gem 'appraisal'
1919
gem 'benchmark-ips'
20+
gem 'benchmark-memory'
2021
gem 'guard'
2122
gem 'guard-rspec'
2223
gem 'guard-rubocop'

benchmark/remounting.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2+
require 'grape'
3+
require 'benchmark/memory'
4+
5+
class VotingApi < Grape::API
6+
logger Logger.new(STDOUT)
7+
8+
helpers do
9+
def logger
10+
VotingApi.logger
11+
end
12+
end
13+
14+
namespace 'votes' do
15+
get do
16+
logger
17+
end
18+
end
19+
end
20+
21+
class PostApi < Grape::API
22+
mount VotingApi
23+
end
24+
25+
class CommentAPI < Grape::API
26+
mount VotingApi
27+
end
28+
29+
env = Rack::MockRequest.env_for('/votes', method: 'GET')
30+
31+
Benchmark.memory do |api|
32+
calls = 1000
33+
34+
api.report('using Array') do
35+
VotingApi.instance_variable_set(:@setup, [])
36+
calls.times { PostApi.call(env) }
37+
puts " setup size: #{VotingApi.instance_variable_get(:@setup).size}"
38+
end
39+
40+
api.report('using Set') do
41+
VotingApi.instance_variable_set(:@setup, Set.new)
42+
calls.times { PostApi.call(env) }
43+
puts " setup size: #{VotingApi.instance_variable_get(:@setup).size}"
44+
end
45+
46+
api.compare!
47+
end

lib/grape/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def inherited(api, base_instance_parent = Grape::API::Instance)
3030
# an instance that will be used to create the set up but will not be mounted
3131
def initial_setup(base_instance_parent)
3232
@instances = []
33-
@setup = []
33+
@setup = Set.new
3434
@base_parent = base_instance_parent
3535
@base_instance = mount_instance
3636
end

spec/grape/api_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,11 @@ def self.io
16001600
expect(subject.io).to receive(:write).with(message)
16011601
subject.logger.info 'this will be logged'
16021602
end
1603+
1604+
it 'does not unnecessarily retain duplicate setup blocks' do
1605+
subject.logger
1606+
expect { subject.logger }.to_not change(subject.instance_variable_get(:@setup), :size)
1607+
end
16031608
end
16041609

16051610
describe '.helpers' do

0 commit comments

Comments
 (0)