Skip to content

Commit

Permalink
Fix array_use_braces for body params (#757)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bikolya authored and LeFnord committed Sep 6, 2019
1 parent 204ced4 commit d7bf077
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

* [#757](https://github.com/ruby-grape/grape-swagger/pull/757): Fix `array_use_braces` for nested body params - [@bikolya](https://github.com/bikolya).
* [#756](https://github.com/ruby-grape/grape-swagger/pull/756): Fix reference creation when custom type for documentation is provided - [@bikolya](https://github.com/bikolya).

### 0.33.0 (June 21, 2019)
Expand Down
15 changes: 9 additions & 6 deletions lib/grape-swagger/endpoint/params_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ def initialize(params, settings)
end

def parse_request_params
array_keys = []
public_params.each_with_object({}) do |(name, options), memo|
name = name.to_s
param_type = options[:type]
param_type = param_type.to_s unless param_type.nil?

if param_type_is_array?(param_type)
array_keys << name
options[:is_array] = true

name += '[]' if array_use_braces?(options)
name += '[]' if array_use_braces?
end

memo[name] = options
Expand All @@ -34,8 +31,8 @@ def parse_request_params

private

def array_use_braces?(options)
settings[:array_use_braces] && !(options[:documentation] && options[:documentation][:param_type] == 'body')
def array_use_braces?
@array_use_braces ||= settings[:array_use_braces] && !includes_body_param?
end

def param_type_is_array?(param_type)
Expand All @@ -61,6 +58,12 @@ def public_parameter?(param)
param_hidden = param_hidden.call if param_hidden.is_a?(Proc)
!param_hidden
end

def includes_body_param?
params.any? do |_, options|
options.dig(:documentation, :param_type) == 'body' || options.dig(:documentation, :in) == 'body'
end
end
end
end
end
46 changes: 43 additions & 3 deletions spec/lib/endpoint/params_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,53 @@
end

context 'when param is nested in a param of hash type' do
let(:params) { [['param_1', { type: 'Hash' }], ['param_1[param_2]', { type: 'String' }]] }
let(:params) { [param_1, param_2] }
let(:param_1) { ['param_1', { type: 'Hash' }] }
let(:param_2) { ['param_1[param_2]', { type: 'String' }] }

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

it 'does not add braces to the param key' do
expect(parse_request_params.keys.last).to eq 'param_1[param_2]'
context 'and param is of simple type' do
it 'does not add braces to the param key' do
expect(parse_request_params.keys.last).to eq 'param_1[param_2]'
end
end

context 'and param is of array type' do
let(:param_2) { ['param_1[param_2]', { type: 'Array[String]' }] }

it 'adds braces to the param key' do
expect(parse_request_params.keys.last).to eq 'param_1[param_2][]'
end

context 'and `param_type` option is set to body' do
let(:param_2) do
['param_1[param_2]', { type: 'Array[String]', documentation: { param_type: 'body' } }]
end

it 'does not add braces to the param key' do
expect(parse_request_params.keys.last).to eq 'param_1[param_2]'
end
end

context 'and `in` option is set to body' do
let(:param_2) do
['param_1[param_2]', { type: 'Array[String]', documentation: { in: 'body' } }]
end

it 'does not add braces to the param key' do
expect(parse_request_params.keys.last).to eq 'param_1[param_2]'
end
end

context 'and hash `param_type` option is set to body' do
let(:param_1) { ['param_1', { type: 'Hash', documentation: { param_type: 'body' } }] }

it 'does not add braces to the param key' do
expect(parse_request_params.keys.last).to eq 'param_1[param_2]'
end
end
end
end
end
Expand Down

0 comments on commit d7bf077

Please sign in to comment.