@@ -598,4 +598,172 @@ def app
598
598
expect ( json . key? ( :artist_id ) ) . not_to be_truthy
599
599
end
600
600
end
601
+
602
+ describe 'parameter renaming' do
603
+ context 'with a renamed hash with nested parameters' do
604
+ before do
605
+ subject . format :json
606
+ subject . params do
607
+ optional :email_address , type : String , regexp : /.+@.+/ , as : :email
608
+ end
609
+ subject . post '/test' do
610
+ declared ( params , include_missing : false )
611
+ end
612
+ end
613
+
614
+ it 'generates the correct parameter names for documentation' do
615
+ expect ( subject . routes . first . params . keys ) . to match ( %w[ email_address ] )
616
+ end
617
+
618
+ it 'maps the renamed parameter correctly (original name)' do
619
+ post '/test' , email_address :
'[email protected] '
620
+ expect ( JSON . parse ( last_response . body ) ) . to \
621
+ match ( 'email' => '[email protected] ' )
622
+ end
623
+
624
+ it 'maps the renamed parameter correctly (as name)' do
625
+ post '/test' , email :
'[email protected] '
626
+ expect ( JSON . parse ( last_response . body ) ) . to \
627
+ match ( 'email' => '[email protected] ' )
628
+ end
629
+
630
+ it 'validates the renamed parameter correctly (original name)' do
631
+ post '/test' , email_address : 'bad[at]example.com'
632
+ expect ( JSON . parse ( last_response . body ) ) . to \
633
+ match ( 'error' => 'email_address is invalid' )
634
+ end
635
+
636
+ it 'validates the renamed parameter correctly (as name)' do
637
+ # NOTE: This is mostly unexpected behaviour, because when we "know" the
638
+ # renamed parameter name we can bypass the parameter validation
639
+ # here as client.
640
+ post '/test' , email : 'bad[at]example.com'
641
+ expect ( JSON . parse ( last_response . body ) ) . to \
642
+ match ( 'email' => 'bad[at]example.com' )
643
+ # Should be: match('error' => 'email_address is invalid')
644
+ end
645
+ end
646
+
647
+ context 'with a renamed hash with nested parameters' do
648
+ before do
649
+ subject . format :json
650
+ subject . params do
651
+ optional :address , type : Hash , as : :address_attributes do
652
+ optional :street , type : String , values : [ 'Street 1' , 'Street 2' ] ,
653
+ default : 'Street 1'
654
+ optional :city , type : String
655
+ end
656
+ end
657
+ subject . post '/test' do
658
+ declared ( params , include_missing : false )
659
+ end
660
+ end
661
+
662
+ it 'generates the correct parameter names for documentation' do
663
+ expect ( subject . routes . first . params . keys ) . to \
664
+ match ( %w[ address address[street] address[city] ] )
665
+ end
666
+
667
+ it 'maps the renamed parameter correctly (original name)' do
668
+ post '/test' , address : { city : 'Berlin' , street : 'Street 2' , t : 't' }
669
+ expect ( JSON . parse ( last_response . body ) ) . to \
670
+ match ( 'address_attributes' => { 'city' => 'Berlin' ,
671
+ 'street' => 'Street 2' } )
672
+ end
673
+
674
+ it 'maps the renamed parameter correctly (as name)' do
675
+ post '/test' , address_attributes : { city : 'Berlin' , unknown : '1' }
676
+ expect ( JSON . parse ( last_response . body ) ) . to \
677
+ match ( 'address_attributes' => { 'city' => 'Berlin' ,
678
+ 'street' => 'Street 1' } )
679
+ end
680
+
681
+ it 'validates the renamed parameter correctly (original name)' do
682
+ post '/test' , address : { street : 'unknown' }
683
+ expect ( JSON . parse ( last_response . body ) ) . to \
684
+ match ( 'error' => 'address[street] does not have a valid value' )
685
+ end
686
+
687
+ it 'validates the renamed parameter correctly (as name)' do
688
+ post '/test' , address_attributes : { street : 'unknown' }
689
+ expect ( JSON . parse ( last_response . body ) ) . to \
690
+ match ( 'error' => 'address[street] does not have a valid value' )
691
+ end
692
+ end
693
+
694
+ context 'with a renamed hash with nested renamed parameter' do
695
+ before do
696
+ subject . format :json
697
+ subject . params do
698
+ optional :user , type : Hash , as : :user_attributes do
699
+ optional :email_address , type : String , regexp : /.+@.+/ , as : :email
700
+ end
701
+ end
702
+ subject . post '/test' do
703
+ declared ( params , include_missing : false )
704
+ end
705
+ end
706
+
707
+ it 'generates the correct parameter names for documentation' do
708
+ expect ( subject . routes . first . params . keys ) . to \
709
+ match ( %w[ user user[email_address] ] )
710
+ end
711
+
712
+ it 'maps the renamed parameter correctly (original name)' do
713
+ post '/test' , user :
{ email_address :
'[email protected] ' }
714
+ expect ( JSON . parse ( last_response . body ) ) . to \
715
+ match ( 'user_attributes' => { 'email' => '[email protected] ' } )
716
+ end
717
+
718
+ it 'maps the renamed parameter correctly (as name, 1)' do
719
+ post '/test' , user :
{ email :
'[email protected] ' }
720
+ expect ( JSON . parse ( last_response . body ) ) . to \
721
+ match ( 'user_attributes' => { 'email' => '[email protected] ' } )
722
+ end
723
+
724
+ it 'maps the renamed parameter correctly (as name, 2)' do
725
+ post '/test' , user_attributes :
{ email_address :
'[email protected] ' }
726
+ expect ( JSON . parse ( last_response . body ) ) . to \
727
+ match ( 'user_attributes' => { 'email' => '[email protected] ' } )
728
+ end
729
+
730
+ it 'maps the renamed parameter correctly (as name, 3)' do
731
+ post '/test' , user_attributes :
{ email :
'[email protected] ' }
732
+ expect ( JSON . parse ( last_response . body ) ) . to \
733
+ match ( 'user_attributes' => { 'email' => '[email protected] ' } )
734
+ end
735
+
736
+ it 'validates the renamed parameter correctly (original name)' do
737
+ post '/test' , user : { email_address : 'bad[at]example.com' }
738
+ expect ( JSON . parse ( last_response . body ) ) . to \
739
+ match ( 'error' => 'user[email_address] is invalid' )
740
+ end
741
+
742
+ it 'validates the renamed parameter correctly (as name, 1)' do
743
+ # NOTE: This is mostly unexpected behaviour, because when we "know" the
744
+ # renamed parameter name we can bypass the parameter validation
745
+ # here as client.
746
+ post '/test' , user : { email : 'bad[at]example.com' }
747
+ expect ( JSON . parse ( last_response . body ) ) . to \
748
+ match ( 'user_attributes' => { 'email' => 'bad[at]example.com' } )
749
+ # Should be: match('error' => 'user[email_address] is invalid')
750
+ end
751
+
752
+ it 'validates the renamed parameter correctly (as name, 2)' do
753
+ post '/test' , user_attributes : { email_address : 'bad[at]example.com' }
754
+ expect ( JSON . parse ( last_response . body ) ) . to \
755
+ match ( 'error' => 'user[email_address] is invalid' )
756
+ end
757
+
758
+ it 'validates the renamed parameter correctly (as name, 3)' do
759
+ # NOTE: This is mostly unexpected behaviour, because when we "know" the
760
+ # renamed parameter name we can bypass the parameter validation
761
+ # here as client.
762
+ post '/test' , user_attributes : { email : 'bad[at]example.com' }
763
+ expect ( JSON . parse ( last_response . body ) ) . to \
764
+ match ( 'user_attributes' => { 'email' => 'bad[at]example.com' } )
765
+ # Should be: match('error' => 'user[email_address] is invalid')
766
+ end
767
+ end
768
+ end
601
769
end
0 commit comments