From 2131dd5032e5b95129d9da4dae918a6a79846655 Mon Sep 17 00:00:00 2001 From: Louis Simoneau Date: Thu, 10 Mar 2016 10:52:36 +1100 Subject: [PATCH] Add test proving leak between Array types and subsequent Hash types This confirms the issue raised in this comment: https://github.com/ruby-grape/grape/issues/1131#issuecomment-194126326 --- spec/grape/validations_spec.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/grape/validations_spec.rb b/spec/grape/validations_spec.rb index 731d803fde..f3898bc05b 100644 --- a/spec/grape/validations_spec.rb +++ b/spec/grape/validations_spec.rb @@ -274,6 +274,28 @@ def define_requires_none end end + # Ensure there is no leakage between declared Array types and + # subsequent Hash types + context 'required with an Array and a Hash block' do + before do + subject.params do + requires :cats, type: Array[String], default: [] + requires :items, type: Hash do + requires :key + end + end + subject.get '/required' do + 'required works' + end + end + + it 'does not output index [0] for Hash types' do + get '/required', cats: ['Garfield'], items: { foo: 'bar' } + expect(last_response.status).to eq(400) + expect(last_response.body).to eq('items[key] is missing') + end + end + context 'required with a Hash block' do before do subject.params do @@ -292,6 +314,12 @@ def define_requires_none expect(last_response.body).to eq('items is missing, items[key] is missing') end + it 'errors when nested param not present' do + get '/required', items: { foo: "bar" } + expect(last_response.status).to eq(400) + expect(last_response.body).to eq('items[key] is missing') + end + it 'errors when param is not a Hash' do get '/required', items: 'hello' expect(last_response.status).to eq(400)