@@ -28,10 +28,14 @@ def app
28
28
optional :nested_arr , type : Array do
29
29
optional :eighth
30
30
end
31
+ optional :empty_arr , type : Array
32
+ optional :empty_hash , type : Hash
31
33
end
32
34
optional :arr , type : Array do
33
35
optional :nineth
34
36
end
37
+ optional :empty_arr , type : Array
38
+ optional :empty_hash , type : Hash
35
39
end
36
40
end
37
41
@@ -103,7 +107,7 @@ def app
103
107
end
104
108
get '/declared?first=present'
105
109
expect ( last_response . status ) . to eq ( 200 )
106
- expect ( JSON . parse ( last_response . body ) . keys . size ) . to eq ( 5 )
110
+ expect ( JSON . parse ( last_response . body ) . keys . size ) . to eq ( 7 )
107
111
end
108
112
109
113
it 'has a optional param with default value all the time' do
@@ -122,7 +126,7 @@ def app
122
126
123
127
get '/declared?first=present&nested[fourth]=1'
124
128
expect ( last_response . status ) . to eq ( 200 )
125
- expect ( JSON . parse ( last_response . body ) [ 'nested' ] . keys . size ) . to eq 4
129
+ expect ( JSON . parse ( last_response . body ) [ 'nested' ] . keys . size ) . to eq 6
126
130
end
127
131
128
132
it 'builds nested params when given array' do
@@ -145,45 +149,53 @@ def app
145
149
expect ( JSON . parse ( last_response . body ) [ 'nested' ] . size ) . to eq 2
146
150
end
147
151
148
- context 'sets nested objects when the param is missing' do
149
- it 'to be a hash when include_missing is true' do
150
- subject . get '/declared' do
151
- declared ( params , include_missing : true )
152
- end
152
+ context 'when the param is missing and include_missing=false' do
153
+ before do
154
+ subject . get ( '/declared' ) { declared ( params , include_missing : false ) }
155
+ end
153
156
157
+ it 'sets nested objects to be nil' do
154
158
get '/declared?first=present'
155
159
expect ( last_response . status ) . to eq ( 200 )
156
- expect ( JSON . parse ( last_response . body ) [ 'nested' ] ) . to eq ( { } )
160
+ expect ( JSON . parse ( last_response . body ) [ 'nested' ] ) . to be_nil
157
161
end
162
+ end
158
163
159
- it 'to be an array when include_missing is true' do
160
- subject . get '/declared' do
161
- declared ( params , include_missing : true )
162
- end
164
+ context 'when the param is missing and include_missing= true' do
165
+ before do
166
+ subject . get ( '/declared' ) { declared ( params , include_missing : true ) }
167
+ end
163
168
169
+ it 'sets objects with type=Hash to be a hash' do
164
170
get '/declared?first=present'
165
171
expect ( last_response . status ) . to eq ( 200 )
166
- expect ( JSON . parse ( last_response . body ) [ 'arr' ] ) . to be_a ( Array )
167
- end
168
172
169
- it 'to be an array when nested and include_missing is true' do
170
- subject . get '/declared' do
171
- declared ( params , include_missing : true )
172
- end
173
+ body = JSON . parse ( last_response . body )
174
+ expect ( body [ 'empty_hash' ] ) . to eq ( { } )
175
+ expect ( body [ 'nested' ] ) . to be_a ( Hash )
176
+ expect ( body [ 'nested' ] [ 'empty_hash' ] ) . to eq ( { } )
177
+ expect ( body [ 'nested' ] [ 'nested_two' ] ) . to be_a ( Hash )
178
+ end
173
179
174
- get '/declared?first=present&nested[fourth]=1'
180
+ it 'sets objects with type=Array to be an array' do
181
+ get '/declared?first=present'
175
182
expect ( last_response . status ) . to eq ( 200 )
176
- expect ( JSON . parse ( last_response . body ) [ 'nested' ] [ 'nested_arr' ] ) . to be_a ( Array )
177
- end
178
183
179
- it 'to be nil when include_missing is false' do
180
- subject . get '/declared' do
181
- declared ( params , include_missing : false )
182
- end
184
+ body = JSON . parse ( last_response . body )
185
+ expect ( body [ 'empty_arr' ] ) . to eq ( [ ] )
186
+ expect ( body [ 'arr' ] ) . to eq ( [ ] )
187
+ expect ( body [ 'nested' ] [ 'empty_arr' ] ) . to eq ( [ ] )
188
+ expect ( body [ 'nested' ] [ 'nested_arr' ] ) . to eq ( [ ] )
189
+ end
183
190
191
+ it 'includes all declared children when type=Hash' do
184
192
get '/declared?first=present'
185
193
expect ( last_response . status ) . to eq ( 200 )
186
- expect ( JSON . parse ( last_response . body ) [ 'nested' ] ) . to be_nil
194
+
195
+ body = JSON . parse ( last_response . body )
196
+ expect ( body [ 'nested' ] . keys ) . to eq ( %w[ fourth fifth nested_two nested_arr empty_arr empty_hash ] )
197
+ expect ( body [ 'nested' ] [ 'nested_two' ] . keys ) . to eq ( %w[ sixth nested_three ] )
198
+ expect ( body [ 'nested' ] [ 'nested_two' ] [ 'nested_three' ] . keys ) . to eq ( %w[ seventh ] )
187
199
end
188
200
end
189
201
0 commit comments