File tree Expand file tree Collapse file tree 2 files changed +37
-9
lines changed
Expand file tree Collapse file tree 2 files changed +37
-9
lines changed Original file line number Diff line number Diff line change @@ -3,21 +3,19 @@ module React
33 # @param props [Object] If it's a Hash or Array, it will be recursed. Otherwise it will be returned.
44 # @return [Hash] a new hash whose keys are camelized strings
55 def self . camelize_props ( props )
6- case props
6+ props_as_json = props . as_json
7+
8+ case props_as_json
79 when Hash
8- props . each_with_object ( { } ) do |( key , value ) , new_props |
10+ props_as_json . each_with_object ( { } ) do |( key , value ) , new_props |
911 new_key = key . to_s . camelize ( :lower )
1012 new_value = camelize_props ( value )
1113 new_props [ new_key ] = new_value
1214 end
1315 when Array
14- props . map { |item | camelize_props ( item ) }
16+ props_as_json . map { |item | camelize_props ( item ) }
1517 else
16- if defined? ( ActionController ::Parameters ) && props . is_a? ( ActionController ::Parameters )
17- camelize_props ( props . to_h )
18- else
19- props
20- end
18+ props_as_json
2119 end
2220 end
2321end
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ def test_it_camelizes_props
2323 [ { 'nestedArray' => { } } ]
2424 ]
2525 } ,
26- 'alreadyCamelized' => :ok
26+ 'alreadyCamelized' => 'ok'
2727 }
2828
2929 assert_equal expected_props , React . camelize_props ( raw_props )
@@ -47,4 +47,34 @@ def test_it_camelizes_params
4747
4848 assert_equal expected_params , React . camelize_props ( permitted_params )
4949 end
50+
51+ def test_it_camelizes_json_serializable_objects
52+ my_json_serializer = Class . new do
53+ def initialize ( data )
54+ @data = data
55+ end
56+
57+ def as_json
58+ @data
59+ end
60+ end
61+
62+ raw_props = {
63+ key_one : 'value1' ,
64+ key_two : my_json_serializer . new (
65+ nested_key_one : 'nested_value1' ,
66+ nested_key_two : [ 'nested' , 'value' , 'two' ]
67+ )
68+ }
69+
70+ expected_params = {
71+ 'keyOne' => 'value1' ,
72+ 'keyTwo' => {
73+ 'nestedKeyOne' => 'nested_value1' ,
74+ 'nestedKeyTwo' => [ 'nested' , 'value' , 'two' ]
75+ }
76+ }
77+
78+ assert_equal expected_params , React . camelize_props ( raw_props )
79+ end
5080end
You can’t perform that action at this time.
0 commit comments