Skip to content

Commit

Permalink
Merge pull request #538 from dm1try/fix_default_values_for_group_params
Browse files Browse the repository at this point in the history
Fix default values for group params
  • Loading branch information
dblock committed Dec 17, 2013
2 parents de7a3c4 + f5b8772 commit 47b6168
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Next Release
* [#500](https://github.com/intridea/grape/pull/500): Skip entity auto-detection when explicitely passed - [@yaneq](https://github.com/yaneq).
* [#503](https://github.com/intridea/grape/pull/503): Calling declared(params) from child namespace fails to include parent namespace defined params - [@myitcv](https://github.com/myitcv).
* [#512](https://github.com/intridea/grape/pull/512): Don't create `Grape::Request` multiple times - [@dblock](https://github.com/dblock).
* [#538](https://github.com/intridea/grape/pull/538): Fix default values for grouped params - [@dm1try](https://github.com/dm1try).
* Your contribution here.


Expand Down
6 changes: 4 additions & 2 deletions lib/grape/validations/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ def validate_param!(attr_name, params)
end

def validate!(params)
params = AttributesIterator.new(self, @scope, params)
params.each do |resource_params, attr_name|
attrs = AttributesIterator.new(self, @scope, params)
parent_element = @scope.element
attrs.each do |resource_params, attr_name|
if resource_params[attr_name].nil?
validate_param!(attr_name, resource_params)
params[parent_element] = resource_params if parent_element
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/grape/validations/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ class API < Grape::API
get '/numbers' do
{ random_number: params[:random], non_random_number: params[:non_random_number] }
end

params do
group :foo do
optional :bar, default: 'foo-bar'
end
end
get '/group' do
{ foo_bar: params[:foo][:bar] }
end
end
end
end
Expand Down Expand Up @@ -83,4 +92,11 @@ def app
before['non_random_number'].should == after['non_random_number']
before['random_number'].should_not == after['random_number']
end

it 'set default values for optional grouped params' do
get('/group')
last_response.status.should == 200
last_response.body.should == { foo_bar: 'foo-bar' }.to_json
end

end

0 comments on commit 47b6168

Please sign in to comment.