Skip to content

Commit 8be46fe

Browse files
committed
Make options endpoints and related components thread-safe
1 parent 5fa9fb1 commit 8be46fe

File tree

7 files changed

+21
-33
lines changed

7 files changed

+21
-33
lines changed

lib/grape/dsl/desc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module Desc
4949
# # ...
5050
# end
5151
#
52-
def desc(description, options = {}, &config_block)
52+
def desc(description, **options, &config_block)
5353
settings =
5454
if config_block
5555
endpoint_config = defined?(configuration) ? configuration : nil

lib/grape/dsl/inside_route.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def version
152152
end
153153

154154
def configuration
155+
pp options[:for].configuration
155156
options[:for].configuration.evaluate
156157
end
157158

lib/grape/dsl/routing.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def mount(mounts, *opts)
141141
# {hello: 'world'}
142142
# end
143143
# end
144-
def route(methods, paths = ['/'], route_options = {}, &block)
144+
def route(methods, paths = ['/'], **route_options, &block)
145145
method = methods == :any ? '*' : methods
146146
endpoint_params = inheritable_setting.namespace_stackable_with_hash(:params) || {}
147147
endpoint_description = inheritable_setting.route[:description]
@@ -156,7 +156,7 @@ def route(methods, paths = ['/'], route_options = {}, &block)
156156
route_options: all_route_options
157157
}
158158

159-
new_endpoint = Grape::Endpoint.new(inheritable_setting, endpoint_options, &block)
159+
new_endpoint = Grape::Endpoint.new(inheritable_setting, **endpoint_options, &block)
160160
endpoints << new_endpoint unless endpoints.any? { |e| e.equals?(new_endpoint) }
161161

162162
inheritable_setting.route_end
@@ -166,7 +166,7 @@ def route(methods, paths = ['/'], route_options = {}, &block)
166166
Grape::HTTP_SUPPORTED_METHODS.each do |supported_method|
167167
define_method supported_method.downcase do |*args, **options, &block|
168168
paths = args.first || ['/']
169-
route(supported_method, paths, options, &block)
169+
route(supported_method, paths, **options, &block)
170170
end
171171
end
172172

@@ -182,7 +182,7 @@ def route(methods, paths = ['/'], route_options = {}, &block)
182182
# # defines the endpoint: GET /foo/bar
183183
# end
184184
# end
185-
def namespace(space = nil, options = {}, &block)
185+
def namespace(space = nil, **options, &block)
186186
return Namespace.joined_space_path(inheritable_setting.namespace_stackable[:namespace]) unless space || block
187187

188188
within_namespace do
@@ -217,9 +217,7 @@ def reset_endpoints!
217217
#
218218
# @param param [Symbol] The name of the parameter you wish to declare.
219219
# @option options [Regexp] You may supply a regular expression that the declared parameter must meet.
220-
def route_param(param, options = {}, &block)
221-
options = options.dup
222-
220+
def route_param(param, **options, &block)
223221
options[:requirements] = {
224222
param.to_sym => options[:requirements]
225223
} if options[:requirements].is_a?(Regexp)
@@ -228,7 +226,7 @@ def route_param(param, options = {}, &block)
228226
requires param, type: options[:type]
229227
end if options.key?(:type)
230228

231-
namespace(":#{param}", options, &block)
229+
namespace(":#{param}", **options, &block)
232230
end
233231

234232
# @return array of defined versions

lib/grape/endpoint.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ def run_before_each(endpoint)
4646
# @note This happens at the time of API definition, so in this context the
4747
# endpoint does not know if it will be mounted under a different endpoint.
4848
# @yield a block defining what your API should do when this endpoint is hit
49-
def initialize(new_settings, options = {}, &block)
50-
require_option(options, :path)
51-
require_option(options, :method)
52-
49+
def initialize(new_settings, **options, &block)
5350
self.inheritable_setting = new_settings.point_in_time_copy
5451

5552
# now +namespace_stackable(:declared_params)+ contains all params defined for
@@ -61,12 +58,11 @@ def initialize(new_settings, options = {}, &block)
6158
inheritable_setting.namespace_stackable[:representations] = [] unless inheritable_setting.namespace_stackable[:representations]
6259
inheritable_setting.namespace_inheritable[:default_error_status] = 500 unless inheritable_setting.namespace_inheritable[:default_error_status]
6360

64-
@options = options
65-
66-
@options[:path] = Array(options[:path])
67-
@options[:path] << '/' if options[:path].empty?
61+
@options = options.dup
62+
@options[:path] = Array(@options[:path])
63+
@options[:path] << '/' if @options[:path].empty?
6864

69-
@options[:method] = Array(options[:method])
65+
@options[:method] = Array(@options[:method])
7066
@options[:route_options] ||= {}
7167

7268
@lazy_initialize_lock = Mutex.new
@@ -89,10 +85,6 @@ def inherit_settings(namespace_stackable)
8985
endpoints&.each { |e| e.inherit_settings(namespace_stackable) }
9086
end
9187

92-
def require_option(options, key)
93-
raise Grape::Exceptions::MissingOption.new(key) unless options.key?(key)
94-
end
95-
9688
def routes
9789
@routes ||= endpoints&.collect(&:routes)&.flatten || to_routes
9890
end

spec/grape/dsl/desc_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
it 'sets a description' do
1515
desc_text = 'The description'
1616
options = { message: 'none' }
17-
subject.desc desc_text, options
17+
subject.desc desc_text, **options
1818
expect(subject.namespace_setting(:description)).to eq(options.merge(description: desc_text))
1919
expect(subject.route_setting(:description)).to eq(options.merge(description: desc_text))
2020
end

spec/grape/dsl/routing_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class << self
138138
expect(endpoint_options[:route_options]).to eq(foo: 'bar', fiz: 'baz', params: { nuz: 'naz' })
139139
end.and_yield
140140

141-
subject.route(:get, '/foo', { foo: 'bar' }, &proc {})
141+
subject.route(:get, '/foo', foo: 'bar', &proc {})
142142
end
143143
end
144144

@@ -257,20 +257,20 @@ class << self
257257
let(:regex) { /(.*)/ }
258258

259259
it 'calls #namespace with given params' do
260-
expect(subject).to receive(:namespace).with(':foo', {}).and_yield
261-
subject.route_param('foo', {}, &proc {})
260+
expect(subject).to receive(:namespace).with(':foo').and_yield
261+
subject.route_param('foo', &proc {})
262262
end
263263

264264
it 'nests requirements option under param name' do
265265
expect(subject).to receive(:namespace) do |_param, options|
266266
expect(options[:requirements][:foo]).to eq regex
267267
end
268-
subject.route_param('foo', options, &proc {})
268+
subject.route_param('foo', **options, &proc {})
269269
end
270270

271271
it 'does not modify options parameter' do
272272
allow(subject).to receive(:namespace)
273-
expect { subject.route_param('foo', options, &proc {}) }
273+
expect { subject.route_param('foo', **options, &proc {}) }
274274
.not_to(change { options })
275275
end
276276
end

spec/grape/endpoint_spec.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ def app
6464
it 'takes a settings stack, options, and a block' do
6565
p = proc {}
6666
expect do
67-
described_class.new(Grape::Util::InheritableSetting.new, {
68-
path: '/',
69-
method: :get
70-
}, &p)
67+
described_class.new(Grape::Util::InheritableSetting.new, path: '/', method: :get, &p)
7168
end.not_to raise_error
7269
end
7370
end
@@ -1136,7 +1133,7 @@ def memoized
11361133
end
11371134

11381135
describe '#inspect' do
1139-
subject { described_class.new(settings, options).inspect }
1136+
subject { described_class.new(settings, **options).inspect }
11401137

11411138
let(:options) do
11421139
{

0 commit comments

Comments
 (0)