Skip to content

Commit f08b62a

Browse files
committed
make sure correct options are passed for nested and respect
include_missing with no parents update changelog remove empty line remove puts statement clarify changelog
1 parent 4004faf commit f08b62a

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [#1555](https://github.com/ruby-grape/grape/pull/1555): Added code coverage w/Coveralls - [@dblock](https://github.com/dblock).
66
* [#1568](https://github.com/ruby-grape/grape/pull/1568): Add `proc` option to `values` validator to allow custom checks - [@jlfaber](https://github.com/jlfaber).
77
* [#1575](https://github.com/ruby-grape/grape/pull/1575): Include nil values for missing nested params in declared - [@thogg4](https://github.com/thogg4).
8+
* [#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).
89
* Your contribution here.
910

1011
#### Fixes

lib/grape/dsl/inside_route.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ def declared_hash(passed_params, options, declared_params)
5050
# If it is not a Hash then it does not have children.
5151
# Find its value or set it to nil.
5252
if !declared_param.is_a?(Hash)
53+
next unless options[:include_missing] || passed_params.key?(declared_param)
5354
memo[optioned_param_key(declared_param, options)] = passed_params[declared_param]
5455
else
5556
declared_param.each_pair do |declared_parent_param, declared_children_params|
5657
next unless options[:include_missing] || passed_params.key?(declared_parent_param)
5758

5859
passed_children_params = passed_params[declared_parent_param] || Hashie::Mash.new
59-
memo[optioned_param_key(declared_parent_param, options)] = declared(passed_children_params, @options, declared_children_params)
60+
memo[optioned_param_key(declared_parent_param, options)] = declared(passed_children_params, options, declared_children_params)
6061
end
6162
end
6263
end

spec/grape/endpoint_spec.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def app
419419
expect(last_response.status).to eq(201)
420420
end
421421

422-
it 'does not include missing attributes when there are nested hashes' do
422+
it 'includes missing attributes with defaults when there are nested hashes' do
423423
subject.get '/dummy' do
424424
end
425425

@@ -450,6 +450,32 @@ def app
450450
expect(json['nested']['nested_nested'].keys).to eq %w(sixth seven)
451451
expect(json['nested']['nested_nested']['sixth']).to eq 'sixth'
452452
end
453+
454+
it 'does not include missing attributes when there are nested hashes' do
455+
subject.get '/dummy' do
456+
end
457+
458+
subject.params do
459+
requires :first
460+
optional :second
461+
optional :third
462+
optional :nested, type: Hash do
463+
optional :fourth
464+
optional :fifth
465+
end
466+
end
467+
468+
subject.get '/declared' do
469+
declared(params, include_missing: false)
470+
end
471+
472+
get '/declared?first=present&nested[fourth]=4'
473+
json = JSON.parse(last_response.body)
474+
expect(last_response.status).to eq(200)
475+
expect(json['first']).to eq 'present'
476+
expect(json['nested'].keys).to eq %w(fourth)
477+
expect(json['nested']['fourth']).to eq '4'
478+
end
453479
end
454480

455481
describe '#declared; call from child namespace' do

0 commit comments

Comments
 (0)