Skip to content

Commit a00b2d8

Browse files
committed
Merge pull request #1225 from playpasshq/hotfix/given-with-nested-params
Fix Given with nested params
2 parents c788a47 + a65d691 commit a00b2d8

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
=============
33

44
* [#1216](https://github.com/ruby-grape/grape/pull/1142): Fix JSON error response when calling `error!` with non-Strings - [@jrforrest](https://github.com/jrforrest).
5+
* [#1225](https://github.com/ruby-grape/grape/pull/1225): Fix `given` with nested params not returning correct declared params - [@JanStevens](https://github.com/JanStevens).
56
* Your contribution here.
67

78
0.14.0 (12/07/2015)

lib/grape/validations/params_scope.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ def required?
9090
# Adds a parameter declaration to our list of validations.
9191
# @param attrs [Array] (see Grape::DSL::Parameters#requires)
9292
def push_declared_params(attrs)
93-
@declared_params.concat attrs
93+
if lateral?
94+
@parent.push_declared_params(attrs)
95+
else
96+
@declared_params.concat attrs
97+
end
9498
end
9599

96100
private
@@ -145,7 +149,14 @@ def new_scope(attrs, optional = false, &block)
145149
end
146150

147151
opts = attrs[1] || { type: Array }
148-
self.class.new(api: @api, element: attrs.first, parent: self, optional: optional, type: opts[:type], &block)
152+
153+
self.class.new(
154+
api: @api,
155+
element: attrs.first,
156+
parent: self,
157+
optional: optional,
158+
type: opts[:type],
159+
&block)
149160
end
150161

151162
# Returns a new parameter scope, not nested under any current-level param

spec/grape/validations/params_scope_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def app
9393
module ParamsScopeSpec
9494
class CustomType
9595
attr_reader :value
96+
9697
def self.parse(value)
9798
fail if value == 'invalid'
9899
new(value)
@@ -327,5 +328,37 @@ def initialize(value)
327328
expect(last_response.status).to eq(400)
328329
expect(last_response.body).to eq('bar[b] is missing')
329330
end
331+
332+
it 'includes the nested parameter within #declared(params)' do
333+
subject.params do
334+
requires :bar, type: Hash do
335+
optional :a
336+
given :a do
337+
requires :b
338+
end
339+
end
340+
end
341+
subject.get('/nested') { declared(params).to_json }
342+
343+
get '/nested', bar: { a: true, b: 'yes' }
344+
expect(JSON.parse(last_response.body)).to eq('bar' => { 'a' => 'true', 'b' => 'yes' })
345+
end
346+
347+
it 'includes level 2 nested parameters outside the given within #declared(params)' do
348+
subject.params do
349+
requires :bar, type: Hash do
350+
optional :a
351+
given :a do
352+
requires :c, type: Hash do
353+
requires :b
354+
end
355+
end
356+
end
357+
end
358+
subject.get('/nested') { declared(params).to_json }
359+
360+
get '/nested', bar: { a: true, c: { b: 'yes' } }
361+
expect(JSON.parse(last_response.body)).to eq('bar' => { 'a' => 'true', 'c' => { 'b' => 'yes' } })
362+
end
330363
end
331364
end

0 commit comments

Comments
 (0)