Skip to content
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

fix evaluate given in array #2287

Merged
merged 6 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* [#2256](https://github.com/ruby-grape/grape/pull/2256): Raise `Grape::Exceptions::MultipartPartLimitError` from Rack when too many files are uploaded - [@bschmeck](https://github.com/bschmeck).
* [#2266](https://github.com/ruby-grape/grape/pull/2266): Fix code coverage - [@duffn](https://github.com/duffn).
* [#2284](https://github.com/ruby-grape/grape/pull/2284): Fix an unexpected backtick - [@zysend](https://github.com/zysend).
* [#2287](https://github.com/ruby-grape/grape/pull/2287): Fix evaluate given in array - [@zysend](https://github.com/zysend).
zysend marked this conversation as resolved.
Show resolved Hide resolved
* Your contribution here.

### 1.6.2 (2021/12/30)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def declared_array(passed_params, options, declared_params, params_nested_path)

def declared_hash(passed_params, options, declared_params, params_nested_path)
declared_params.each_with_object(passed_params.class.new) do |declared_param_attr, memo|
next if options[:evaluate_given] && !declared_param_attr.meets_dependency?(options[:request_params])
next if options[:evaluate_given] && !declared_param_attr.scope.meets_dependency?(passed_params, options[:request_params])

declared_hash_attr(passed_params, options, declared_param_attr.key, params_nested_path, memo)
end
Expand Down
6 changes: 0 additions & 6 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ def initialize(key, scope)
@scope = scope
end

def meets_dependency?(request_params)
return true if scope.nil?

scope.meets_dependency?(scope.params(request_params), request_params)
end

# @return Array[Symbol, Hash[Symbol => Array]] declared_params with symbol instead of Attr
def self.attrs_keys(declared_params)
declared_params.map do |declared_param_attr|
Expand Down
28 changes: 28 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,34 @@ def initialize(value)
end
end

context 'lateral parameter within an array param' do
before do
[true, false].each do |evaluate_given|
subject.params do
optional :array, type: Array do
optional :a
given a: ->(a) { a == 'a' } do
optional :b
end
end
end
subject.get("/evaluate_given_#{evaluate_given}") do
declared(params, evaluate_given: evaluate_given).to_json
end
end
end

it 'evaluate_given_false' do
get '/evaluate_given_false', array: [{ a: 'c', b: 'b' }, { a: 'a', b: 'b' }]
expect(JSON.parse(last_response.body)).to eq('array' => [{ 'a' => 'c', 'b' => 'b' }, { 'a' => 'a', 'b' => 'b' }])
end

it 'evaluate_given_true' do
get '/evaluate_given_true', array: [{ a: 'c', b: 'b' }, { a: 'a', b: 'b' }]
expect(JSON.parse(last_response.body)).to eq('array' => [{ 'a' => 'c' }, { 'a' => 'a', 'b' => 'b' }])
end
end

context 'nested parameter' do
before do
[true, false].each do |evaluate_given|
Expand Down