File tree Expand file tree Collapse file tree 3 files changed +33
-10
lines changed Expand file tree Collapse file tree 3 files changed +33
-10
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ Next Release
3
3
4
4
* [ #1503 ] ( https://github.com/ruby-grape/grape/pull/1503 ) : Allow to use regexp validator with arrays - [ @akoltun ] ( https://github.com/akoltun ) .
5
5
* [ #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 ) .
6
7
* Your contribution here.
7
8
8
9
0.18.0 (10/7/2016)
Original file line number Diff line number Diff line change @@ -193,21 +193,24 @@ def declared_param?(param)
193
193
194
194
alias group requires
195
195
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
+
196
208
# @param params [Hash] initial hash of parameters
197
209
# @return hash of parameters relevant for the current scope
198
210
# @api private
199
211
def params ( params )
200
212
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
211
214
params
212
215
end
213
216
end
Original file line number Diff line number Diff line change @@ -585,6 +585,25 @@ def initialize(value)
585
585
expect ( JSON . parse ( last_response . body ) ) . to eq ( 'bar' => { 'a' => 'x' , 'c' => { 'b' => 'yes' } } )
586
586
end
587
587
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
+
588
607
context 'failing fast' do
589
608
context 'when fail_fast is not defined' do
590
609
it 'does not stop validation' do
You can’t perform that action at this time.
0 commit comments