Skip to content

Commit e14fc46

Browse files
ericproulxclaude
andcommitted
Report a nested Array scope that got no Array under its own name only
An Array scope inside an Array scope is read from each element of the outer Array. When an element left that param out, or held something other than an Array there, such as `0`, AttributesIterator yielded the value as an element of the inner Array. The inner scope's validators then reported a second error, named with the outer element's index for the inner Array and a stale index for the outer one: requires :points, type: Array do requires :location, type: Array do requires :id, type: Integer end end {"points": [{"location": [{"id": 1}]}, {"location": [{"id": 1}, {"id": 2}]}, {"location": 0}]} Before: points[2][location] is invalid, points[1][location][2][id] is missing, points[1][location][2][id] is invalid After: points[2][location] is invalid The iterator now passes over such a value, as #each already does at the top for an Array scope that did not get an Array. The scope's own validators still report it, so these requests answer 400 as before, with the errors the same scope gives at the root. Five specs had pinned the extra error and are updated. Fixes #1998. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 19e5dad commit e14fc46

2 files changed

Lines changed: 43 additions & 13 deletions

File tree

‎lib/grape/validations/attributes_iterator.rb‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ def do_each(params_to_process, tracker, index_scope, parent_indices, sent_elemen
8181
# its elements deeper than that is yielded as-is, so the attribute
8282
# validators see a non-hash and fail it the same way any other
8383
# unexpected element type does.
84-
if resource_params.is_a?(Array) && parent_indices.size < @max_nesting
84+
if parent_indices.size < @max_nesting
85+
# Short of that depth, a value is an Array scope's own param in an
86+
# element of an Array further up. One that is not an Array was left
87+
# out or given as something else, which that scope's own validators
88+
# report; it has no elements, as #each finds at the top. Yielding it
89+
# as one reported a second error, under the index of the Array above.
90+
next unless resource_params.is_a?(Array)
91+
8592
nested_indices = [index] + parent_indices
8693
do_each(resource_params, tracker, index_scope, nested_indices, sent_elements?(resource_params, nested_indices), &)
8794
next

‎spec/grape/validations_spec.rb‎

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,35 @@ def define_optional_using
294294
end
295295
end
296296

297+
context 'requires an Array nested in an Array, given something else' do
298+
before do
299+
subject.params do
300+
requires :points, type: Array do
301+
requires :location, type: Array do
302+
requires :id, type: Integer
303+
end
304+
end
305+
end
306+
subject.post('/') { 'ok' }
307+
end
308+
309+
it 'reports a value that is not an Array under its own name only' do
310+
body = { points: [{ location: [{ id: 1 }] }, { location: [{ id: 1 }, { id: 2 }] }, { location: 0 }] }
311+
post '/', body.to_json, 'CONTENT_TYPE' => 'application/json'
312+
313+
expect(last_response.status).to eq(400)
314+
expect(last_response.body).to eq('points[2][location] is invalid')
315+
end
316+
317+
it 'reports one left out under its own name only' do
318+
body = { points: [{ location: [{ id: 1 }, { id: 2 }] }, {}] }
319+
post '/', body.to_json, 'CONTENT_TYPE' => 'application/json'
320+
321+
expect(last_response.status).to eq(400)
322+
expect(last_response.body).to eq('points[1][location] is missing')
323+
end
324+
end
325+
297326
context 'requires :all using Grape::Entity documentation' do
298327
def define_requires_all
299328
documentation = {
@@ -755,7 +784,7 @@ def validate_param!(attr_name, params)
755784
# NOTE: with body parameters in json or XML or similar this
756785
# should actually fail with: children[parents][name] is missing.
757786
expect(last_response.status).to eq(400)
758-
expect(last_response.body).to eq('children[1][parents] is missing, children[0][parents][1][name] is missing, children[0][parents][1][name] is empty')
787+
expect(last_response.body).to eq('children[1][parents] is missing')
759788
end
760789

761790
it 'errors when a parameter is not present in array within array' do
@@ -786,17 +815,11 @@ def validate_param!(attr_name, params)
786815
# should actually return 200, since an empty array is valid.
787816
get '/within_array', children: []
788817
expect(last_response.status).to eq(400)
789-
expect(last_response.body).to eq(
790-
'children[0][name] is missing, ' \
791-
'children[0][parents] is missing, ' \
792-
'children[0][parents] is invalid, ' \
793-
'children[0][parents][0][name] is missing, ' \
794-
'children[0][parents][0][name] is empty'
795-
)
818+
expect(last_response.body).to eq('children[0][name] is missing, children[0][parents] is missing, children[0][parents] is invalid')
796819

797820
get '/within_array', children: [{ name: 'Jay' }]
798821
expect(last_response.status).to eq(400)
799-
expect(last_response.body).to eq('children[0][parents] is missing, children[0][parents][0][name] is missing, children[0][parents][0][name] is empty')
822+
expect(last_response.body).to eq('children[0][parents] is missing')
800823
end
801824

802825
it 'errors when param is not an Array' do
@@ -944,7 +967,7 @@ def validate_param!(attr_name, params)
944967
expect(last_response.status).to eq(200)
945968
put_with_json '/within_array', children: [{ name: 'Jay' }]
946969
expect(last_response.status).to eq(400)
947-
expect(last_response.body).to eq('children[0][parents] is missing, children[0][parents][0][name] is missing')
970+
expect(last_response.body).to eq('children[0][parents] is missing')
948971
end
949972
end
950973

@@ -1085,7 +1108,7 @@ def validate_param!(attr_name, params)
10851108
it 'does internal validations if the outer group is present' do
10861109
get '/nested_optional_group', items: [{ key: 'foo' }]
10871110
expect(last_response.status).to eq(400)
1088-
expect(last_response.body).to eq('items[0][required_subitems] is missing, items[0][required_subitems][0][value] is missing')
1111+
expect(last_response.body).to eq('items[0][required_subitems] is missing')
10891112

10901113
get '/nested_optional_group', items: [{ key: 'foo', required_subitems: [{ value: 'bar' }] }]
10911114
expect(last_response.status).to eq(200)
@@ -1105,7 +1128,7 @@ def validate_param!(attr_name, params)
11051128
it 'handles validation within arrays' do
11061129
get '/nested_optional_group', items: [{ key: 'foo' }]
11071130
expect(last_response.status).to eq(400)
1108-
expect(last_response.body).to eq('items[0][required_subitems] is missing, items[0][required_subitems][0][value] is missing')
1131+
expect(last_response.body).to eq('items[0][required_subitems] is missing')
11091132

11101133
get '/nested_optional_group', items: [{ key: 'foo', required_subitems: [{ value: 'bar' }] }]
11111134
expect(last_response.status).to eq(200)

0 commit comments

Comments
 (0)