Skip to content

Commit 7879468

Browse files
committed
Merge pull request #994 from u2/missing_optional_array_params
Fix optional Array params default to Hash
2 parents 8ee8be7 + f60e58e commit 7879468

File tree

6 files changed

+46
-6
lines changed

6 files changed

+46
-6
lines changed

.rubocop_todo.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This configuration was generated by `rubocop --auto-gen-config`
2-
# on 2014-12-16 11:52:50 -0500 using RuboCop version 0.28.0.
2+
# on 2014-12-16 11:52:50 -0500 using RuboCop version 0.29.1.
33
# The point is for the user to remove these configuration records
44
# one by one as the offenses are removed from the code base.
55
# Note that changes in the inspected code, or installation of new
@@ -30,7 +30,7 @@ Metrics/ClassLength:
3030

3131
# Offense count: 15
3232
Metrics/CyclomaticComplexity:
33-
Max: 19
33+
Max: 20
3434

3535
# Offense count: 545
3636
# Configuration parameters: AllowURI, URISchemes.
@@ -44,7 +44,7 @@ Metrics/MethodLength:
4444

4545
# Offense count: 13
4646
Metrics/PerceivedComplexity:
47-
Max: 21
47+
Max: 22
4848

4949
# Offense count: 26
5050
# Cop supports --auto-correct.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#### Fixes
1515

16+
* [#994](https://github.com/intridea/grape/pull/994): Fixed optional Array params default to Hash - [@u2](https://github.com/u2).
1617
* [#988](https://github.com/intridea/grape/pull/988): Fixed duplicate identical endpoints - [@u2](https://github.com/u2).
1718
* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).
1819
* [#942](https://github.com/intridea/grape/pull/942): Fixed forced presence for optional params when based on a reused entity that was also required in another context - [@croeck](https://github.com/croeck).

gemfiles/rails_3.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ source 'https://rubygems.org'
55
gem 'rails', '3.2.19'
66

77
group :development, :test do
8-
gem 'rubocop', '~> 0.28.0'
8+
gem 'rubocop', '~> 0.29.1'
99
gem 'guard'
1010
gem 'guard-rspec'
1111
gem 'guard-rubocop'

gemfiles/rails_4.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ source 'https://rubygems.org'
55
gem 'rails', '4.1.6'
66

77
group :development, :test do
8-
gem 'rubocop', '~> 0.28.0'
8+
gem 'rubocop', '~> 0.29.1'
99
gem 'guard'
1010
gem 'guard-rspec'
1111
gem 'guard-rubocop'

lib/grape/dsl/inside_route.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def declared(params, options = {}, declared_params = nil)
4343

4444
if params.key?(parent) || options[:include_missing]
4545
hash[output_key] = if children
46-
declared(params[parent] || {}, options, Array(children))
46+
children_params = params[parent] || (children.is_a?(Array) ? [] : {})
47+
declared(children_params, options, Array(children))
4748
else
4849
params[parent]
4950
end

spec/grape/endpoint_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,44 @@ def app
295295
expect(inner_params[:nested].size).to eq 2
296296
end
297297

298+
it 'builds nested params' do
299+
inner_params = nil
300+
subject.get '/declared' do
301+
inner_params = declared(params)
302+
''
303+
end
304+
305+
get '/declared?first=present&nested[fourth]=1'
306+
expect(last_response.status).to eq(200)
307+
expect(inner_params[:nested].keys.size).to eq 1
308+
end
309+
310+
context 'sets nested array when the param is missing' do
311+
it 'to be array when include_missing is true' do
312+
inner_params = nil
313+
subject.get '/declared' do
314+
inner_params = declared(params, include_missing: true)
315+
''
316+
end
317+
318+
get '/declared?first=present'
319+
expect(last_response.status).to eq(200)
320+
expect(inner_params[:nested]).to be_a(Array)
321+
end
322+
323+
it 'to be nil when include_missing is false' do
324+
inner_params = nil
325+
subject.get '/declared' do
326+
inner_params = declared(params, include_missing: false)
327+
''
328+
end
329+
330+
get '/declared?first=present'
331+
expect(last_response.status).to eq(200)
332+
expect(inner_params[:nested]).to be_nil
333+
end
334+
end
335+
298336
it 'filters out any additional params that are given' do
299337
inner_params = nil
300338
subject.get '/declared' do

0 commit comments

Comments
 (0)