Skip to content

Commit

Permalink
failing tests for arrays nested within hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
dleavitt committed Feb 28, 2020
1 parent 53d826f commit 034ed90
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Expand Up @@ -395,6 +395,60 @@ def initialize(value)
end
end

context 'nested structures within hashes' do
it 'allows nesting of optional arrays within hashes' do
subject.params do
optional :data, type: Hash do
optional :array, type: Array do
optional :name, type: String
end
end
end
subject.post('/nesty') { declared(params).to_json }

post '/nesty', { data: {} }
expect(last_response.status).to eq 201
# {"data"=>{"array"=>{"name"=>nil}}}
expect(JSON.parse(last_response.body)['data']['array']).not_to be_a Hash
end

it 'allows nesting of optional arrays within hashes, JSON-style' do
subject.format :json
subject.params do
optional :data, type: JSON do
optional :array, type: Array[JSON] do
optional :name, type: String
end
end
end
subject.post('/nesty') { declared(params) }

body = { data: { } }.to_json
post '/nesty', body, "CONTENT_TYPE" => "application/json"

expect(last_response.status).to eq 201
# {"data"=>{"array"=>{"name"=>nil}}}
expect(JSON.parse(last_response.body)['data']['array']).not_to be_a Hash
end

it "doesn't try to add deeply nested optional hashes" do
# this may be working as intended?
subject.params do
optional :data, type: JSON do
optional :json, type: JSON do
optional :name, type: String
end
end
end
subject.post('/nesty') { declared(params).to_json }

post '/nesty', { data: { } }
expect(last_response.status).to eq 201
# {"data"=>{"json"=>{"name"=>nil}}}
expect(JSON.parse(last_response.body)['data']['json']).not_to be_a Hash
end
end

context 'when validations are dependent on a parameter' do
before do
subject.params do
Expand Down

0 comments on commit 034ed90

Please sign in to comment.