Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Params: optional arrays within hashes/JSON objects broken #2001

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess here we should expect the hash. @dleavitt could you verify this test?

end
end

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