Skip to content

Commit 277831b

Browse files
committed
Fix array_use_braces for body params
Previously this is what was generated: `"attr":{"type":"array","items":{"type":"object","properties":{"":{"type":"string"}}}}` instead of `"attr":{"type":"array","items":{"type":"string"}}` in case this attribute was nested in a Hash attribute with "body" in `param_type`
1 parent 445d2d4 commit 277831b

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#### Fixes
88

9-
* Your contribution here.
9+
* [#757](https://github.com/ruby-grape/grape-swagger/pull/757): Fix `array_use_braces` for nested body params - [@bikolya](https://github.com/bikolya).
1010

1111
### 0.33.0 (June 21, 2019)
1212

lib/grape-swagger/endpoint/params_parser.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@ def initialize(params, settings)
1515
end
1616

1717
def parse_request_params
18-
array_keys = []
1918
public_params.each_with_object({}) do |(name, options), memo|
2019
name = name.to_s
2120
param_type = options[:type]
2221
param_type = param_type.to_s unless param_type.nil?
2322

2423
if param_type_is_array?(param_type)
25-
array_keys << name
2624
options[:is_array] = true
27-
28-
name += '[]' if array_use_braces?(options)
25+
name += '[]' if array_use_braces?
2926
end
3027

3128
memo[name] = options
@@ -34,8 +31,8 @@ def parse_request_params
3431

3532
private
3633

37-
def array_use_braces?(options)
38-
settings[:array_use_braces] && !(options[:documentation] && options[:documentation][:param_type] == 'body')
34+
def array_use_braces?
35+
@array_use_braces ||= settings[:array_use_braces] && !includes_body_param?
3936
end
4037

4138
def param_type_is_array?(param_type)
@@ -61,6 +58,12 @@ def public_parameter?(param)
6158
param_hidden = param_hidden.call if param_hidden.is_a?(Proc)
6259
!param_hidden
6360
end
61+
62+
def includes_body_param?
63+
params.any? do |_, options|
64+
options.dig(:documentation, :param_type) == 'body' || options.dig(:documentation, :in) == 'body'
65+
end
66+
end
6467
end
6568
end
6669
end

spec/lib/endpoint/params_parser_spec.rb

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,53 @@
5656
end
5757

5858
context 'when param is nested in a param of hash type' do
59-
let(:params) { [['param_1', { type: 'Hash' }], ['param_1[param_2]', { type: 'String' }]] }
59+
let(:params) { [param_1, param_2] }
60+
let(:param_1) { ['param_1', { type: 'Hash' }] }
61+
let(:param_2) { ['param_1[param_2]', { type: 'String' }] }
6062

6163
context 'and array_use_braces setting set to true' do
6264
let(:settings) { { array_use_braces: true } }
6365

64-
it 'does not add braces to the param key' do
65-
expect(parse_request_params.keys.last).to eq 'param_1[param_2]'
66+
context 'and param is of simple type' do
67+
it 'does not add braces to the param key' do
68+
expect(parse_request_params.keys.last).to eq 'param_1[param_2]'
69+
end
70+
end
71+
72+
context 'and param is of array type' do
73+
let(:param_2) { ['param_1[param_2]', { type: 'Array[String]' }] }
74+
75+
it 'adds braces to the param key' do
76+
expect(parse_request_params.keys.last).to eq 'param_1[param_2][]'
77+
end
78+
79+
context 'and `param_type` option is set to body' do
80+
let(:param_2) do
81+
['param_1[param_2]', { type: 'Array[String]', documentation: { param_type: 'body' }}]
82+
end
83+
84+
it 'does not add braces to the param key' do
85+
expect(parse_request_params.keys.last).to eq 'param_1[param_2]'
86+
end
87+
end
88+
89+
context 'and `in` option is set to body' do
90+
let(:param_2) do
91+
['param_1[param_2]', { type: 'Array[String]', documentation: { in: 'body' }}]
92+
end
93+
94+
it 'does not add braces to the param key' do
95+
expect(parse_request_params.keys.last).to eq 'param_1[param_2]'
96+
end
97+
end
98+
99+
context 'and hash `param_type` option is set to body' do
100+
let(:param_1) { ['param_1', { type: 'Hash', documentation: { param_type: 'body' }}] }
101+
102+
it 'does not add braces to the param key' do
103+
expect(parse_request_params.keys.last).to eq 'param_1[param_2]'
104+
end
105+
end
66106
end
67107
end
68108
end

0 commit comments

Comments
 (0)