Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array_use_braces for body params #757

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?
Copy link
Member

@LeFnord LeFnord Sep 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are the settings come from, is it configurable?
if yes, then it should be described in the README

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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