Skip to content

Commit

Permalink
make sure correct options are passed for nested and respect
Browse files Browse the repository at this point in the history
include_missing with no parents

update changelog

remove empty line

remove puts statement

clarify changelog
  • Loading branch information
thogg4 committed Mar 1, 2017
1 parent 4004faf commit f08b62a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
* [#1555](https://github.com/ruby-grape/grape/pull/1555): Added code coverage w/Coveralls - [@dblock](https://github.com/dblock).
* [#1568](https://github.com/ruby-grape/grape/pull/1568): Add `proc` option to `values` validator to allow custom checks - [@jlfaber](https://github.com/jlfaber).
* [#1575](https://github.com/ruby-grape/grape/pull/1575): Include nil values for missing nested params in declared - [@thogg4](https://github.com/thogg4).
* [#1585](https://github.com/ruby-grape/grape/pull/1585): Bugs in declared method - make sure correct options var is used and respect include missing for non children params - [@thogg4](https://github.com/thogg4).
* Your contribution here.

#### Fixes
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/dsl/inside_route.rb
Expand Up @@ -50,13 +50,14 @@ def declared_hash(passed_params, options, declared_params)
# If it is not a Hash then it does not have children.
# Find its value or set it to nil.
if !declared_param.is_a?(Hash)
next unless options[:include_missing] || passed_params.key?(declared_param)
memo[optioned_param_key(declared_param, options)] = passed_params[declared_param]
else
declared_param.each_pair do |declared_parent_param, declared_children_params|
next unless options[:include_missing] || passed_params.key?(declared_parent_param)

passed_children_params = passed_params[declared_parent_param] || Hashie::Mash.new
memo[optioned_param_key(declared_parent_param, options)] = declared(passed_children_params, @options, declared_children_params)
memo[optioned_param_key(declared_parent_param, options)] = declared(passed_children_params, options, declared_children_params)
end
end
end
Expand Down
28 changes: 27 additions & 1 deletion spec/grape/endpoint_spec.rb
Expand Up @@ -419,7 +419,7 @@ def app
expect(last_response.status).to eq(201)
end

it 'does not include missing attributes when there are nested hashes' do
it 'includes missing attributes with defaults when there are nested hashes' do
subject.get '/dummy' do
end

Expand Down Expand Up @@ -450,6 +450,32 @@ def app
expect(json['nested']['nested_nested'].keys).to eq %w(sixth seven)
expect(json['nested']['nested_nested']['sixth']).to eq 'sixth'
end

it 'does not include missing attributes when there are nested hashes' do
subject.get '/dummy' do
end

subject.params do
requires :first
optional :second
optional :third
optional :nested, type: Hash do
optional :fourth
optional :fifth
end
end

subject.get '/declared' do
declared(params, include_missing: false)
end

get '/declared?first=present&nested[fourth]=4'
json = JSON.parse(last_response.body)
expect(last_response.status).to eq(200)
expect(json['first']).to eq 'present'
expect(json['nested'].keys).to eq %w(fourth)
expect(json['nested']['fourth']).to eq '4'
end
end

describe '#declared; call from child namespace' do
Expand Down

0 comments on commit f08b62a

Please sign in to comment.