@@ -251,16 +251,34 @@ def app
251
251
252
252
describe '#declared' do
253
253
before do
254
+ subject . format :json
254
255
subject . params do
255
256
requires :first
256
257
optional :second
257
258
optional :third , default : 'third-default'
258
259
optional :nested , type : Hash do
259
260
optional :fourth
261
+ optional :fifth
262
+ optional :nested_two , type : Hash do
263
+ optional :sixth
264
+ optional :nested_three , type : Hash do
265
+ optional :seventh
266
+ end
267
+ end
260
268
end
261
269
end
262
270
end
263
271
272
+ it 'should show nil for nested params if include_missing is true' do
273
+ subject . get '/declared' do
274
+ declared ( params , include_missing : true )
275
+ end
276
+
277
+ get '/declared?first=present'
278
+ expect ( last_response . status ) . to eq ( 200 )
279
+ expect ( JSON . parse ( last_response . body ) [ 'nested' ] [ 'fourth' ] ) . to be_nil
280
+ end
281
+
264
282
it 'does not work in a before filter' do
265
283
subject . before do
266
284
declared ( params )
@@ -273,37 +291,31 @@ def app
273
291
end
274
292
275
293
it 'has as many keys as there are declared params' do
276
- inner_params = nil
277
294
subject . get '/declared' do
278
- inner_params = declared ( params ) . keys
279
- ''
295
+ declared ( params )
280
296
end
281
297
get '/declared?first=present'
282
298
expect ( last_response . status ) . to eq ( 200 )
283
- expect ( inner_params . size ) . to eq ( 4 )
299
+ expect ( JSON . parse ( last_response . body ) . keys . size ) . to eq ( 4 )
284
300
end
285
301
286
302
it 'has a optional param with default value all the time' do
287
- inner_params = nil
288
303
subject . get '/declared' do
289
- inner_params = declared ( params )
290
- ''
304
+ declared ( params )
291
305
end
292
306
get '/declared?first=one'
293
307
expect ( last_response . status ) . to eq ( 200 )
294
- expect ( inner_params [ : third] ) . to eql ( 'third-default' )
308
+ expect ( JSON . parse ( last_response . body ) [ ' third' ] ) . to eql ( 'third-default' )
295
309
end
296
310
297
311
it 'builds nested params' do
298
- inner_params = nil
299
312
subject . get '/declared' do
300
- inner_params = declared ( params )
301
- ''
313
+ declared ( params )
302
314
end
303
315
304
316
get '/declared?first=present&nested[fourth]=1'
305
317
expect ( last_response . status ) . to eq ( 200 )
306
- expect ( inner_params [ : nested] . keys . size ) . to eq 1
318
+ expect ( JSON . parse ( last_response . body ) [ ' nested' ] . keys . size ) . to eq 3
307
319
end
308
320
309
321
it 'builds nested params when given array' do
@@ -317,76 +329,54 @@ def app
317
329
optional :fourth
318
330
end
319
331
end
320
- inner_params = nil
321
332
subject . get '/declared' do
322
- inner_params = declared ( params )
323
- ''
333
+ declared ( params )
324
334
end
325
335
326
336
get '/declared?first=present&nested[][fourth]=1&nested[][fourth]=2'
327
337
expect ( last_response . status ) . to eq ( 200 )
328
- expect ( inner_params [ : nested] . size ) . to eq 2
338
+ expect ( JSON . parse ( last_response . body ) [ ' nested' ] . size ) . to eq 2
329
339
end
330
340
331
- it 'builds nested params' do
332
- inner_params = nil
333
- subject . get '/declared' do
334
- inner_params = declared ( params )
335
- ''
336
- end
337
-
338
- get '/declared?first=present&nested[fourth]=1'
339
- expect ( last_response . status ) . to eq ( 200 )
340
- expect ( inner_params [ :nested ] . keys . size ) . to eq 1
341
- end
342
-
343
- context 'sets nested array when the param is missing' do
341
+ context 'sets nested hash when the param is missing' do
344
342
it 'to be array when include_missing is true' do
345
- inner_params = nil
346
343
subject . get '/declared' do
347
- inner_params = declared ( params , include_missing : true )
348
- ''
344
+ declared ( params , include_missing : true )
349
345
end
350
346
351
347
get '/declared?first=present'
352
348
expect ( last_response . status ) . to eq ( 200 )
353
- expect ( inner_params [ : nested] ) . to be_a ( Array )
349
+ expect ( JSON . parse ( last_response . body ) [ ' nested' ] ) . to be_a ( Hash )
354
350
end
355
351
356
352
it 'to be nil when include_missing is false' do
357
- inner_params = nil
358
353
subject . get '/declared' do
359
- inner_params = declared ( params , include_missing : false )
360
- ''
354
+ declared ( params , include_missing : false )
361
355
end
362
356
363
357
get '/declared?first=present'
364
358
expect ( last_response . status ) . to eq ( 200 )
365
- expect ( inner_params [ : nested] ) . to be_nil
359
+ expect ( JSON . parse ( last_response . body ) [ ' nested' ] ) . to be_nil
366
360
end
367
361
end
368
362
369
363
it 'filters out any additional params that are given' do
370
- inner_params = nil
371
364
subject . get '/declared' do
372
- inner_params = declared ( params )
373
- ''
365
+ declared ( params )
374
366
end
375
367
get '/declared?first=one&other=two'
376
368
expect ( last_response . status ) . to eq ( 200 )
377
- expect ( inner_params . key? ( :other ) ) . to eq false
369
+ expect ( JSON . parse ( last_response . body ) . key? ( :other ) ) . to eq false
378
370
end
379
371
380
372
it 'stringifies if that option is passed' do
381
- inner_params = nil
382
373
subject . get '/declared' do
383
- inner_params = declared ( params , stringify : true )
384
- ''
374
+ declared ( params , stringify : true )
385
375
end
386
376
387
377
get '/declared?first=one&other=two'
388
378
expect ( last_response . status ) . to eq ( 200 )
389
- expect ( inner_params [ 'first' ] ) . to eq 'one'
379
+ expect ( JSON . parse ( last_response . body ) [ 'first' ] ) . to eq 'one'
390
380
end
391
381
392
382
it 'does not include missing attributes if that option is passed' do
@@ -447,20 +437,18 @@ def app
447
437
end
448
438
end
449
439
450
- inner_params = nil
451
440
subject . get '/declared' do
452
- inner_params = declared ( params , include_missing : false )
453
- ''
441
+ declared ( params , include_missing : false )
454
442
end
455
443
456
444
get '/declared?first=present&nested[fourth]=&nested[nested_nested][sixth]=sixth'
457
-
445
+ json = JSON . parse ( last_response . body )
458
446
expect ( last_response . status ) . to eq ( 200 )
459
- expect ( inner_params [ : first] ) . to eq 'present'
460
- expect ( inner_params [ : nested] . keys ) . to eq %w( fourth fifth nested_nested )
461
- expect ( inner_params [ : nested] [ : fourth] ) . to eq ''
462
- expect ( inner_params [ : nested] [ : nested_nested] . keys ) . to eq %w( sixth seven )
463
- expect ( inner_params [ : nested] [ : nested_nested] [ : sixth] ) . to eq 'sixth'
447
+ expect ( json [ ' first' ] ) . to eq 'present'
448
+ expect ( json [ ' nested' ] . keys ) . to eq %w( fourth fifth nested_nested )
449
+ expect ( json [ ' nested' ] [ ' fourth' ] ) . to eq ''
450
+ expect ( json [ ' nested' ] [ ' nested_nested' ] . keys ) . to eq %w( sixth seven )
451
+ expect ( json [ ' nested' ] [ ' nested_nested' ] [ ' sixth' ] ) . to eq 'sixth'
464
452
end
465
453
end
466
454
0 commit comments