Skip to content

Commit 157b1dd

Browse files
committed
Refactor param parsers
1 parent c413dc9 commit 157b1dd

File tree

5 files changed

+36
-52
lines changed

5 files changed

+36
-52
lines changed

lib/grape-swagger/request_param_parser_registry.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,30 @@ def initialize
1919
end
2020

2121
def register(klass)
22+
remove_parser(klass)
2223
@parsers << klass
2324
end
2425

2526
def insert_before(before_klass, klass)
26-
insert_at = @parsers.index(before_klass)
27-
insert_at = @parsers.length - 1 if insert_at.nil?
27+
remove_parser(klass)
28+
insert_at = @parsers.index(before_klass) || @parsers.size
2829
@parsers.insert(insert_at, klass)
2930
end
3031

3132
def insert_after(after_klass, klass)
33+
remove_parser(klass)
3234
insert_at = @parsers.index(after_klass)
33-
insert_at = @parsers.length - 1 if insert_at.nil?
34-
@parsers.insert(insert_at + 1, klass)
35+
@parsers.insert(insert_at ? insert_at + 1 : @parsers.size, klass)
3536
end
3637

37-
def each
38-
@parsers.each do |klass|
39-
yield klass
40-
end
38+
def each(&)
39+
@parsers.each(&)
40+
end
41+
42+
private
43+
44+
def remove_parser(klass)
45+
@parsers.reject! { |k| k == klass }
4146
end
4247
end
4348
end

lib/grape-swagger/request_param_parsers/body.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ def initialize(_route, params, settings, endpoint)
1818
def parse
1919
public_params.each_with_object({}) do |(name, options), memo|
2020
name = name.to_s
21-
param_type = options[:type]
22-
param_type = param_type.to_s unless param_type.nil?
21+
param_type = options[:type]&.to_s
2322

24-
if param_type_is_array?(param_type)
23+
if array_param?(param_type)
2524
options[:is_array] = true
2625
name += '[]' if array_use_braces?
2726
end
@@ -36,8 +35,8 @@ def array_use_braces?
3635
@array_use_braces ||= settings[:array_use_braces] && !includes_body_param?
3736
end
3837

39-
def param_type_is_array?(param_type)
40-
return false unless param_type
38+
def array_param?(param_type)
39+
return false if param_type.nil?
4140
return true if param_type == 'Array'
4241

4342
param_types = param_type.match(/\[(.*)\]$/)

lib/grape-swagger/request_param_parsers/headers.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@ def parse
1717
return {} unless route.headers
1818

1919
route.headers.each_with_object({}) do |(name, definition), accum|
20-
params = {
21-
documentation: {
22-
desc: definition['description'] || definition[:description],
23-
in: 'header'
24-
}
25-
}
26-
params[:type] = definition[:type].titleize if definition[:type]
20+
# Extract the description from any key type (string or symbol)
21+
description = definition[:description] || definition['description']
22+
doc = { desc: description, in: 'header' }
2723

28-
accum[name] = definition
29-
.symbolize_keys
30-
.except(:description, 'description')
31-
.merge(params)
24+
header_attrs = definition.symbolize_keys.except(:description, 'description')
25+
header_attrs[:type] = definition[:type].titleize if definition[:type]
26+
header_attrs[:documentation] = doc
27+
28+
accum[name] = header_attrs
3229
end
3330
end
3431
end

lib/grape-swagger/request_param_parsers/route.rb

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def initialize(route, _params, _settings, _endpoint)
1818
def parse
1919
stackable_values = route.app&.inheritable_setting&.namespace_stackable
2020

21-
get_path_params(stackable_values)
2221
path_params = build_path_params(stackable_values)
2322

2423
fulfill_params(path_params)
@@ -54,29 +53,13 @@ def fulfill_params(path_params)
5453
# The route.params hash includes both parametrized params (with a string as a key)
5554
# and well-defined params from body/query (with a symbol as a key).
5655
# We avoid overriding well-defined params with parametrized ones.
57-
next if param.is_a?(String) && accum.key?(param.to_sym)
56+
key = param.is_a?(String) ? param.to_sym : param
57+
next if param.is_a?(String) && accum.key?(key)
5858

59-
value = (path_params[param] || {}).merge(
60-
definition.is_a?(Hash) ? definition : {}
61-
)
62-
63-
accum[param.to_sym] = value.empty? ? DEFAULT_PARAM_TYPE : value
64-
end
65-
end
66-
67-
# Iterates over namespaces recursively
68-
# to build a hash of path params with options, including type
69-
def get_path_params(stackable_values)
70-
params = {}
71-
return param unless stackable_values
72-
return params unless stackable_values.is_a? Grape::Util::StackableValues
73-
74-
stackable_values&.new_values&.dig(:namespace)&.each do |namespace| # rubocop:disable Style/SafeNavigationChainLength
75-
space = namespace.space.to_s.gsub(':', '')
76-
params[space] = namespace.options || {}
59+
defined_options = definition.is_a?(Hash) ? definition : {}
60+
value = (path_params[param] || {}).merge(defined_options)
61+
accum[key] = value.empty? ? DEFAULT_PARAM_TYPE : value
7762
end
78-
inherited_params = get_path_params(stackable_values.inherited_values)
79-
inherited_params.merge(params)
8063
end
8164
end
8265
end

spec/lib/request_param_parsers/body_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,16 @@
109109
end
110110
end
111111

112-
describe '#param_type_is_array?' do
112+
describe '#array_param?' do
113113
it 'returns true if the value passed represents an array' do
114-
expect(parser.send(:param_type_is_array?, 'Array')).to be_truthy
115-
expect(parser.send(:param_type_is_array?, '[String]')).to be_truthy
116-
expect(parser.send(:param_type_is_array?, 'Array[Integer]')).to be_truthy
114+
expect(parser.send(:array_param?, 'Array')).to be_truthy
115+
expect(parser.send(:array_param?, '[String]')).to be_truthy
116+
expect(parser.send(:array_param?, 'Array[Integer]')).to be_truthy
117117
end
118118

119119
it 'returns false if the value passed does not represent an array' do
120-
expect(parser.send(:param_type_is_array?, 'String')).to be_falsey
121-
expect(parser.send(:param_type_is_array?, '[String, Integer]')).to be_falsey
120+
expect(parser.send(:array_param?, 'String')).to be_falsey
121+
expect(parser.send(:array_param?, '[String, Integer]')).to be_falsey
122122
end
123123
end
124124
end

0 commit comments

Comments
 (0)