Skip to content

Commit d3e8de5

Browse files
Handle json array
1 parent 430a45d commit d3e8de5

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

lib/grape/dsl/parameters.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def map_params(params, element, is_array = false)
251251
# @return hash of parameters relevant for the current scope
252252
# @api private
253253
def params(params)
254-
params = @parent.params(params) if instance_variable_defined?(:@parent) && @parent
254+
params = @parent.params_meeting_dependency.presence || @parent.params(params) if instance_variable_defined?(:@parent) && @parent
255255
params = map_params(params, @element) if instance_variable_defined?(:@element) && @element
256256
params
257257
end

lib/grape/validations/params_scope.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Grape
44
module Validations
55
class ParamsScope
66
attr_accessor :element, :parent, :index
7-
attr_reader :type
7+
attr_reader :type, :params_meeting_dependency
88

99
include Grape::DSL::Parameters
1010

@@ -67,6 +67,7 @@ def initialize(opts, &block)
6767
@type = opts[:type]
6868
@group = opts[:group]
6969
@dependent_on = opts[:dependent_on]
70+
@params_meeting_dependency = []
7071
@declared_params = []
7172
@index = nil
7273

@@ -94,7 +95,11 @@ def should_validate?(parameters)
9495
def meets_dependency?(params, request_params)
9596
return true unless @dependent_on
9697
return false if @parent.present? && !@parent.meets_dependency?(@parent.params(request_params), request_params)
97-
return params.any? { |param| meets_dependency?(param, request_params) } if params.is_a?(Array)
98+
99+
if params.is_a?(Array)
100+
@params_meeting_dependency = params.filter { |param| meets_dependency?(param, request_params) }
101+
return @params_meeting_dependency.present?
102+
end
98103

99104
meets_hash_dependency?(params)
100105
end

spec/grape/dsl/parameters_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ def extract_message_option(attrs)
259259
parent_params = { foo: 'bar' }
260260
subject.parent = Object.new
261261
allow(subject.parent).to receive(:params).and_return(parent_params)
262+
allow(subject.parent).to receive(:params_meeting_dependency).and_return(nil)
262263
expect(subject.params({})).to eq parent_params
263264
end
264265

spec/grape/validations/params_scope_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,59 @@ def initialize(value)
630630
expect(last_response.body).to eq 'inner3[0][baz][0][baz_category] is missing'
631631
end
632632

633+
context 'detect when json array' do
634+
before do
635+
subject.params do
636+
requires :array, type: Array do
637+
requires :a, type: String
638+
given a: ->(val) { val == 'a' } do
639+
requires :json, type: Hash do
640+
requires :b, type: String
641+
end
642+
end
643+
end
644+
end
645+
646+
subject.post '/nested_array' do
647+
'nested array works!'
648+
end
649+
end
650+
651+
it 'succeeds' do
652+
params = {
653+
array: [
654+
{
655+
a: 'a',
656+
json: { b: 'b' }
657+
},
658+
{
659+
a: 'b'
660+
}
661+
]
662+
}
663+
post '/nested_array', params.to_json, 'CONTENT_TYPE' => 'application/json'
664+
665+
expect(last_response.status).to eq(201)
666+
end
667+
668+
it 'fails' do
669+
params = {
670+
array: [
671+
{
672+
a: 'a',
673+
json: { b: 'b' }
674+
},
675+
{
676+
a: 'a'
677+
}
678+
]
679+
}
680+
post '/nested_array', params.to_json, 'CONTENT_TYPE' => 'application/json'
681+
682+
expect(last_response.status).to eq(400)
683+
end
684+
end
685+
633686
it 'includes the parameter within #declared(params)' do
634687
get '/test', a: true, b: true
635688

0 commit comments

Comments
 (0)