Skip to content

Commit

Permalink
Merge pull request #994 from u2/missing_optional_array_params
Browse files Browse the repository at this point in the history
Fix optional Array params default to Hash
  • Loading branch information
dblock committed Apr 20, 2015
2 parents 8ee8be7 + f60e58e commit 7879468
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2014-12-16 11:52:50 -0500 using RuboCop version 0.28.0.
# on 2014-12-16 11:52:50 -0500 using RuboCop version 0.29.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -30,7 +30,7 @@ Metrics/ClassLength:

# Offense count: 15
Metrics/CyclomaticComplexity:
Max: 19
Max: 20

# Offense count: 545
# Configuration parameters: AllowURI, URISchemes.
Expand All @@ -44,7 +44,7 @@ Metrics/MethodLength:

# Offense count: 13
Metrics/PerceivedComplexity:
Max: 21
Max: 22

# Offense count: 26
# Cop supports --auto-correct.
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#### Fixes

* [#994](https://github.com/intridea/grape/pull/994): Fixed optional Array params default to Hash - [@u2](https://github.com/u2).
* [#988](https://github.com/intridea/grape/pull/988): Fixed duplicate identical endpoints - [@u2](https://github.com/u2).
* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).
* [#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).
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source 'https://rubygems.org'
gem 'rails', '3.2.19'

group :development, :test do
gem 'rubocop', '~> 0.28.0'
gem 'rubocop', '~> 0.29.1'
gem 'guard'
gem 'guard-rspec'
gem 'guard-rubocop'
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_4.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source 'https://rubygems.org'
gem 'rails', '4.1.6'

group :development, :test do
gem 'rubocop', '~> 0.28.0'
gem 'rubocop', '~> 0.29.1'
gem 'guard'
gem 'guard-rspec'
gem 'guard-rubocop'
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def declared(params, options = {}, declared_params = nil)

if params.key?(parent) || options[:include_missing]
hash[output_key] = if children
declared(params[parent] || {}, options, Array(children))
children_params = params[parent] || (children.is_a?(Array) ? [] : {})
declared(children_params, options, Array(children))
else
params[parent]
end
Expand Down
38 changes: 38 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,44 @@ def app
expect(inner_params[:nested].size).to eq 2
end

it 'builds nested params' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params)
''
end

get '/declared?first=present&nested[fourth]=1'
expect(last_response.status).to eq(200)
expect(inner_params[:nested].keys.size).to eq 1
end

context 'sets nested array when the param is missing' do
it 'to be array when include_missing is true' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params, include_missing: true)
''
end

get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(inner_params[:nested]).to be_a(Array)
end

it 'to be nil when include_missing is false' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params, include_missing: false)
''
end

get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(inner_params[:nested]).to be_nil
end
end

it 'filters out any additional params that are given' do
inner_params = nil
subject.get '/declared' do
Expand Down

0 comments on commit 7879468

Please sign in to comment.