Skip to content

Commit 028c10e

Browse files
author
Tim Connor
committed
Fix bug with handling array params.
Fixes an issue introduced in 7e43215 that results in Array leaf params being ignored. For example, given the following API and Request: ``` class Api < Grape::API params do optional :array, type: Array end post 'example' do declared(params, include_missing: true) end end POST /example { "array": ["value"] } ``` Expected Response Body: ``` { "array": ["value"] } ``` Observed Response Body: ``` { "array": [] } ```
1 parent f1aaab6 commit 028c10e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/grape/dsl/inside_route.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ def declared_hash(passed_params, options, declared_params, params_nested_path)
7575

7676
params_nested_path_dup = params_nested_path.dup
7777
params_nested_path_dup << declared_param.to_s
78-
79-
memo[memo_key] = handle_passed_param(params_nested_path_dup) do
78+
memo[memo_key] = passed_param || handle_passed_param(params_nested_path_dup) do
8079
passed_param
8180
end
8281
end

spec/grape/endpoint/declared_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ def app
135135
expect(JSON.parse(last_response.body)['nested'].keys.size).to eq 9
136136
end
137137

138+
it 'builds arrays correctly' do
139+
subject.params do
140+
requires :first
141+
optional :second, type: Array
142+
end
143+
subject.post('/declared') { declared(params) }
144+
145+
post '/declared', first: 'present', second: ['present']
146+
expect(last_response.status).to eq(201)
147+
148+
body = JSON.parse(last_response.body)
149+
expect(body['second']).to eq(['present'])
150+
end
151+
138152
it 'builds nested params when given array' do
139153
subject.get '/dummy' do
140154
end

0 commit comments

Comments
 (0)