Skip to content

Commit a9c3d2e

Browse files
committed
Add test proving leak between Array types and subsequent Hash types
This confirms the issue raised in this comment: #1131 (comment)
1 parent f33cf12 commit a9c3d2e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

spec/grape/validations_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,28 @@ def define_requires_none
274274
end
275275
end
276276

277+
# Ensure there is no leakage between declared Array types and
278+
# subsequent Hash types
279+
context 'required with an Array and a Hash block' do
280+
before do
281+
subject.params do
282+
requires :cats, type: Array[String], default: []
283+
requires :items, type: Hash do
284+
requires :key
285+
end
286+
end
287+
subject.get '/required' do
288+
'required works'
289+
end
290+
end
291+
292+
it 'does not output index [0] for Hash types' do
293+
get '/required', cats: ['Garfield'], items: { foo: 'bar' }
294+
expect(last_response.status).to eq(400)
295+
expect(last_response.body).to eq('items[key] is missing')
296+
end
297+
end
298+
277299
context 'required with a Hash block' do
278300
before do
279301
subject.params do
@@ -292,6 +314,12 @@ def define_requires_none
292314
expect(last_response.body).to eq('items is missing, items[key] is missing')
293315
end
294316

317+
it 'errors when nested param not present' do
318+
get '/required', items: { foo: 'bar' }
319+
expect(last_response.status).to eq(400)
320+
expect(last_response.body).to eq('items[key] is missing')
321+
end
322+
295323
it 'errors when param is not a Hash' do
296324
get '/required', items: 'hello'
297325
expect(last_response.status).to eq(400)

0 commit comments

Comments
 (0)