@@ -419,6 +419,23 @@ class User
419
419
expect ( JSON . parse ( last_response . body ) ) . to eq ( [ 0 , 0 , 0 , 0 ] )
420
420
end
421
421
422
+ it 'parses parameters even if type is valid' do
423
+ subject . params do
424
+ requires :values , type : Array , coerce_with : -> ( array ) { array . map { |val | val . to_i + 1 } }
425
+ end
426
+ subject . get '/ints' do
427
+ params [ :values ]
428
+ end
429
+
430
+ get '/ints' , values : [ 1 , 2 , 3 , 4 ]
431
+ expect ( last_response . status ) . to eq ( 200 )
432
+ expect ( JSON . parse ( last_response . body ) ) . to eq ( [ 2 , 3 , 4 , 5 ] )
433
+
434
+ get '/ints' , values : %w( a b c d )
435
+ expect ( last_response . status ) . to eq ( 200 )
436
+ expect ( JSON . parse ( last_response . body ) ) . to eq ( [ 1 , 1 , 1 , 1 ] )
437
+ end
438
+
422
439
it 'uses parse where available' do
423
440
subject . params do
424
441
requires :ints , type : Array , coerce_with : JSON do
@@ -477,7 +494,7 @@ class User
477
494
end
478
495
479
496
context 'first-class JSON' do
480
- it 'parses objects and arrays' do
497
+ it 'parses objects, hashes, and arrays' do
481
498
subject . params do
482
499
requires :splines , type : JSON do
483
500
requires :x , type : Integer , values : [ 1 , 2 , 3 ]
@@ -499,17 +516,33 @@ class User
499
516
expect ( last_response . status ) . to eq ( 200 )
500
517
expect ( last_response . body ) . to eq ( 'woof' )
501
518
519
+ get '/' , splines : { x : 1 , ints : [ 1 , 2 , 3 ] , obj : { y : 'woof' } }
520
+ expect ( last_response . status ) . to eq ( 200 )
521
+ expect ( last_response . body ) . to eq ( 'woof' )
522
+
502
523
get '/' , splines : '[{"x":2,"ints":[]},{"x":3,"ints":[4],"obj":{"y":"quack"}}]'
503
524
expect ( last_response . status ) . to eq ( 200 )
504
525
expect ( last_response . body ) . to eq ( 'arrays work' )
505
526
527
+ get '/' , splines : [ { x : 2 , ints : [ ] } , { x : 3 , ints : [ 4 ] , obj : { y : 'quack' } } ]
528
+ expect ( last_response . status ) . to eq ( 200 )
529
+ expect ( last_response . body ) . to eq ( 'arrays work' )
530
+
506
531
get '/' , splines : '{"x":4,"ints":[2]}'
507
532
expect ( last_response . status ) . to eq ( 400 )
508
533
expect ( last_response . body ) . to eq ( 'splines[x] does not have a valid value' )
509
534
535
+ get '/' , splines : { x : 4 , ints : [ 2 ] }
536
+ expect ( last_response . status ) . to eq ( 400 )
537
+ expect ( last_response . body ) . to eq ( 'splines[x] does not have a valid value' )
538
+
510
539
get '/' , splines : '[{"x":1,"ints":[]},{"x":4,"ints":[]}]'
511
540
expect ( last_response . status ) . to eq ( 400 )
512
541
expect ( last_response . body ) . to eq ( 'splines[x] does not have a valid value' )
542
+
543
+ get '/' , splines : [ { x : 1 , ints : [ ] } , { x : 4 , ints : [ ] } ]
544
+ expect ( last_response . status ) . to eq ( 400 )
545
+ expect ( last_response . body ) . to eq ( 'splines[x] does not have a valid value' )
513
546
end
514
547
515
548
it 'works when declared optional' do
0 commit comments