Skip to content

Commit 5d0537f

Browse files
committed
failing tests for arrays nested within hashes
1 parent 53d826f commit 5d0537f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

spec/grape/validations/params_scope_spec.rb

+54
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,60 @@ def initialize(value)
395395
end
396396
end
397397

398+
context 'nested structures within hashes' do
399+
it 'allows nesting of optional arrays within hashes' do
400+
subject.params do
401+
optional :data, type: Hash do
402+
optional :array, type: Array do
403+
optional :name, type: String
404+
end
405+
end
406+
end
407+
subject.post('/nesty') { declared(params).to_json }
408+
409+
post '/nesty', { data: {} }
410+
expect(last_response.status).to eq 201
411+
# {"data"=>{"array"=>{"name"=>nil}}}
412+
expect(JSON.parse(last_response.body)['data']['array']).not_to be_a Hash
413+
end
414+
415+
it 'allows nesting of optional arrays within hashes, JSON-style' do
416+
subject.format :json
417+
subject.params do
418+
optional :data, type: JSON do
419+
optional :array, type: Array[JSON] do
420+
optional :name, type: String
421+
end
422+
end
423+
end
424+
subject.post('/nesty') { declared(params) }
425+
426+
body = { data: {} }.to_json
427+
post '/nesty', body, "CONTENT_TYPE" => "application/json"
428+
429+
expect(last_response.status).to eq 201
430+
# {"data"=>{"array"=>{"name"=>nil}}}
431+
expect(JSON.parse(last_response.body)['data']['array']).not_to be_a Hash
432+
end
433+
434+
it "doesn't try to add deeply nested optional hashes" do
435+
# this may be working as intended?
436+
subject.params do
437+
optional :data, type: JSON do
438+
optional :json, type: JSON do
439+
optional :name, type: String
440+
end
441+
end
442+
end
443+
subject.post('/nesty') { declared(params).to_json }
444+
445+
post '/nesty', { data: {} }
446+
expect(last_response.status).to eq 201
447+
# {"data"=>{"json"=>{"name"=>nil}}}
448+
expect(JSON.parse(last_response.body)['data']['json']['nested_array']).not_to be_a Hash
449+
end
450+
end
451+
398452
context 'when validations are dependent on a parameter' do
399453
before do
400454
subject.params do

0 commit comments

Comments
 (0)