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 optional Array params default to Hash #994

Merged
merged 1 commit into from
Apr 20, 2015
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
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