diff --git a/CHANGELOG.md b/CHANGELOG.md index dd1576850..8fe9bc311 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/grape/dsl/inside_route.rb b/lib/grape/dsl/inside_route.rb index 0e771504a..5cc6e13be 100644 --- a/lib/grape/dsl/inside_route.rb +++ b/lib/grape/dsl/inside_route.rb @@ -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 diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index 3ce801d48..f8a59e729 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -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 @@ -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