Skip to content

Commit fe6a4a4

Browse files
authored
Skip to set default value unless meets_dependency? (#2097)
1 parent 6deb8f4 commit fe6a4a4

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [#2083](https://github.com/ruby-grape/grape/pull/2083): Set `Cache-Control` header only for streamed responses - [@stanhu](https://github.com/stanhu).
1212
* [#2092](https://github.com/ruby-grape/grape/pull/2092): Correct an example params in Include Missing doc - [@huyvohcmc](https://github.com/huyvohcmc).
1313
* [#2091](https://github.com/ruby-grape/grape/pull/2091): Fix ruby 2.7 keyword deprecations - [@dim](https://github.com/dim).
14+
* [#2097](https://github.com/ruby-grape/grape/pull/2097): Skip to set default value unless `meets_dependency?` - [@wanabe](https://github.com/wanabe).
1415

1516
### 1.4.0 (2020/07/10)
1617

lib/grape/validations/validators/default.rb

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def validate_param!(attr_name, params)
2121
def validate!(params)
2222
attrs = SingleAttributeIterator.new(self, @scope, params)
2323
attrs.each do |resource_params, attr_name|
24+
next unless @scope.meets_dependency?(resource_params, params)
2425
validate_param!(attr_name, resource_params) if resource_params.is_a?(Hash) && resource_params[attr_name].nil?
2526
end
2627
end

spec/grape/validations/validators/default_spec.rb

+49
Original file line numberDiff line numberDiff line change
@@ -419,4 +419,53 @@ def app
419419
end
420420
end
421421
end
422+
423+
context 'array with default values and given conditions' do
424+
subject do
425+
Class.new(Grape::API) do
426+
default_format :json
427+
end
428+
end
429+
430+
def app
431+
subject
432+
end
433+
434+
it 'applies the default values only if the conditions are met' do
435+
subject.params do
436+
requires :ary, type: Array do
437+
requires :has_value, type: Grape::API::Boolean
438+
given has_value: ->(has_value) { has_value } do
439+
optional :type, type: String, values: %w[str int], default: 'str'
440+
given type: ->(type) { type == 'str' } do
441+
optional :str, type: String, default: 'a'
442+
end
443+
given type: ->(type) { type == 'int' } do
444+
optional :int, type: Integer, default: 1
445+
end
446+
end
447+
end
448+
end
449+
subject.post('/nested_given_and_default') { declared(self.params) }
450+
451+
params = {
452+
ary: [
453+
{ has_value: false },
454+
{ has_value: true, type: 'int', int: 123 },
455+
{ has_value: true, type: 'str', str: 'b' }
456+
]
457+
}
458+
expected = {
459+
'ary' => [
460+
{ 'has_value' => false, 'type' => nil, 'int' => nil, 'str' => nil },
461+
{ 'has_value' => true, 'type' => 'int', 'int' => 123, 'str' => nil },
462+
{ 'has_value' => true, 'type' => 'str', 'int' => nil, 'str' => 'b' }
463+
]
464+
}
465+
466+
post '/nested_given_and_default', params
467+
expect(last_response.status).to eq(201)
468+
expect(JSON.parse(last_response.body)).to eq(expected)
469+
end
470+
end
422471
end

0 commit comments

Comments
 (0)