Skip to content

Adds failing spec for ruby-grape/grape#1239 #1267

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

Closed
wants to merge 1 commit into from
Closed
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
73 changes: 46 additions & 27 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,12 @@ def app
requires :first
optional :second
optional :third, default: 'third-default'
optional :nested, type: Hash do
optional :hash, type: Hash do
optional :fourth
end
optional :array, type: Array do
optional :fifth
end
end
end

Expand All @@ -279,7 +282,7 @@ def app
end
get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(inner_params.size).to eq(4)
expect(inner_params.size).to eq(5)
end

it 'has a optional param with default value all the time' do
Expand All @@ -300,31 +303,21 @@ def app
''
end

get '/declared?first=present&nested[fourth]=1'
get '/declared?first=present&hash[fourth]=1'
expect(last_response.status).to eq(200)
expect(inner_params[:nested].keys.size).to eq 1
expect(inner_params[:hash].keys.size).to eq 1
end

it 'builds nested params when given array' do
subject.get '/dummy' do
end
subject.params do
requires :first
optional :second
optional :third, default: 'third-default'
optional :nested, type: Array do
optional :fourth
end
end
inner_params = nil
subject.get '/declared' do
inner_params = declared(params)
''
end

get '/declared?first=present&nested[][fourth]=1&nested[][fourth]=2'
get '/declared?first=present&array[][fourth]=1&array[][fourth]=2'
expect(last_response.status).to eq(200)
expect(inner_params[:nested].size).to eq 2
expect(inner_params[:array].size).to eq 2
end

it 'builds nested params' do
Expand All @@ -334,9 +327,9 @@ def app
''
end

get '/declared?first=present&nested[fourth]=1'
get '/declared?first=present&hash[fourth]=1'
expect(last_response.status).to eq(200)
expect(inner_params[:nested].keys.size).to eq 1
expect(inner_params[:hash].keys.size).to eq 1
end

context 'sets nested array when the param is missing' do
Expand All @@ -349,7 +342,33 @@ def app

get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(inner_params[:nested]).to be_a(Array)
expect(inner_params[:array]).to be_a(Array)
end

it 'to be nil when include_missing is false' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params, include_missing: false)
''
end

get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(inner_params[:array]).to be_nil
end
end

context 'sets nested hash when the param is missing' do
it 'to be a hash when include missing is true' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params, include_missing: true)
''
end

get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(inner_params[:hash]).to be_a(Hash)
end

it 'to be nil when include_missing is false' do
Expand All @@ -361,7 +380,7 @@ def app

get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(inner_params[:nested]).to be_nil
expect(inner_params[:hash]).to be_nil
end
end

Expand Down Expand Up @@ -436,10 +455,10 @@ def app
requires :first
optional :second
optional :third, default: nil
optional :nested, type: Hash do
optional :hash, type: Hash do
optional :fourth, default: nil
optional :fifth, default: nil
requires :nested_nested, type: Hash do
requires :nested_hash, type: Hash do
optional :sixth, default: 'sixth-default'
optional :seven, default: nil
end
Expand All @@ -452,14 +471,14 @@ def app
''
end

get '/declared?first=present&nested[fourth]=&nested[nested_nested][sixth]=sixth'
get '/declared?first=present&hash[fourth]=&hash[nested_hash][sixth]=sixth'

expect(last_response.status).to eq(200)
expect(inner_params[:first]).to eq 'present'
expect(inner_params[:nested].keys).to eq %w(fourth fifth nested_nested)
expect(inner_params[:nested][:fourth]).to eq ''
expect(inner_params[:nested][:nested_nested].keys).to eq %w(sixth seven)
expect(inner_params[:nested][:nested_nested][:sixth]).to eq 'sixth'
expect(inner_params[:hash].keys).to eq %w(fourth fifth nested_hash)
expect(inner_params[:hash][:fourth]).to eq ''
expect(inner_params[:hash][:nested_hash].keys).to eq %w(sixth seven)
expect(inner_params[:hash][:nested_hash][:sixth]).to eq 'sixth'
end
end

Expand Down