Skip to content

Commit aa81fff

Browse files
krbsdblock
authored andcommitted
Fix: deeply nested parameters within #declared(params) not included (#1512)
1 parent 3a34ca2 commit aa81fff

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Next Release
33

44
* [#1503](https://github.com/ruby-grape/grape/pull/1503): Allow to use regexp validator with arrays - [@akoltun](https://github.com/akoltun).
55
* [#1507](https://github.com/ruby-grape/grape/pull/1507): Add group attributes for parameter definitions - [@304](https://github.com/304).
6+
* [#1512](https://github.com/ruby-grape/grape/pull/1512): Fix for deeply nested params TypeError situation - [@krbs](https://github.com/krbs).
67
* Your contribution here.
78

89
0.18.0 (10/7/2016)

lib/grape/dsl/parameters.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,24 @@ def declared_param?(param)
193193

194194
alias group requires
195195

196+
def map_params(params, element)
197+
if params.is_a?(Array)
198+
params.map do |el|
199+
map_params(el, element)
200+
end
201+
elsif params.is_a?(Hash)
202+
params[element] || {}
203+
else
204+
{}
205+
end
206+
end
207+
196208
# @param params [Hash] initial hash of parameters
197209
# @return hash of parameters relevant for the current scope
198210
# @api private
199211
def params(params)
200212
params = @parent.params(params) if @parent
201-
if @element
202-
params = if params.is_a?(Array)
203-
# used for calculating parent array indices for error messages
204-
params.map { |el| el[@element] || {} }
205-
elsif params.is_a?(Hash)
206-
params[@element] || {}
207-
else
208-
{}
209-
end
210-
end
213+
params = map_params(params, @element) if @element
211214
params
212215
end
213216
end

spec/grape/validations/params_scope_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,25 @@ def initialize(value)
585585
expect(JSON.parse(last_response.body)).to eq('bar' => { 'a' => 'x', 'c' => { 'b' => 'yes' } })
586586
end
587587

588+
it 'includes deeply nested parameters within #declared(params)' do
589+
subject.params do
590+
requires :arr1, type: Array do
591+
requires :hash1, type: Hash do
592+
requires :arr2, type: Array do
593+
requires :hash2, type: Hash do
594+
requires :something, type: String
595+
end
596+
end
597+
end
598+
end
599+
end
600+
subject.get('/nested_deep') { declared(params).to_json }
601+
602+
get '/nested_deep', arr1: [{ hash1: { arr2: [{ hash2: { something: 'value' } }] } }]
603+
expect(last_response.status).to eq(200)
604+
expect(JSON.parse(last_response.body)).to eq('arr1' => [{ 'hash1' => { 'arr2' => [{ 'hash2' => { 'something' => 'value' } }] } }])
605+
end
606+
588607
context 'failing fast' do
589608
context 'when fail_fast is not defined' do
590609
it 'does not stop validation' do

0 commit comments

Comments
 (0)