Skip to content

Commit 00c2a4e

Browse files
ericproulxclaude
andauthored
Validate the params of an optional Hash scope without the iterator (#3075)
A validator reads its scope's params directly, and skips the attributes iterator, when its scope is always validated: the root scope and the required Hash scopes under it. A Hash scope that is optional, or that sits under an optional one, went through the iterator instead, although it has nothing to add there either: #validate only goes on once the scope's params are there and not blank, so the iterator hands that one Hash back, has no empty value to pass over and no dependency to meet. The direct path now covers every scope with no Array scope and no `given` on its chain. It no longer tries a required Hash scope inside an Array scope, whose params resolve to an Array and were resolved a second time by the iterator. Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 0964ce5 commit 00c2a4e

3 files changed

Lines changed: 16 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* [#3071](https://github.com/ruby-grape/grape/pull/3071): Skip the query parse when building the params of a request with no query string - [@ericproulx](https://github.com/ericproulx).
3737
* [#3072](https://github.com/ruby-grape/grape/pull/3072): Answer the `Accept` lists axios, jQuery and HTTPie send without negotiating them on every request - [@ericproulx](https://github.com/ericproulx).
3838
* [#3073](https://github.com/ruby-grape/grape/pull/3073): Let a `rescue_from :all` in a namespace or mounted API outrank the enclosing scope's handlers - [@ericproulx](https://github.com/ericproulx).
39+
* [#3075](https://github.com/ruby-grape/grape/pull/3075): Validate the params of an optional Hash scope without the attributes iterator - [@ericproulx](https://github.com/ericproulx).
3940
* [#3076](https://github.com/ruby-grape/grape/pull/3076): Skip the query merge when building the params of a body sent without a query string - [@ericproulx](https://github.com/ericproulx).
4041
* Your contribution here.
4142

‎lib/grape/validations/validators/base.rb‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,19 @@ def validate_param!(attr_name, params)
121121
# time a validator is built. See #validate and #validate!.
122122
def choose_validation_path(scope)
123123
@always_validated = scope.always_validated?
124-
@direct = @always_validated && !scope.iterates_elements?
124+
@direct = scope.independent? && scope.array_depth.zero?
125125
@direct_elements = scope.validated_when_given? && scope.iterates_elements? && scope.array_depth == 1
126126
end
127127

128-
# #validate! on a scope that always validates and does not iterate
129-
# elements, once its params resolved to a Hash: the root scope and the
130-
# required Hash scopes under it, which is most validators of most
131-
# endpoints. There the iterator hands back that Hash alone, and every
132-
# scope on the chain is required with no dependency, so the checks
133-
# around the attribute loop drop out. The machinery cost more than
134-
# the validation itself.
128+
# #validate! on a scope with no Array scope and no +given+ on its
129+
# chain, once its params resolved to a Hash: the root scope and the
130+
# Hash scopes under it, required or optional, which is most validators
131+
# of most endpoints. There the iterator hands back that Hash alone,
132+
# and no scope on the chain has a dependency to meet. An optional one
133+
# is only passed over when its params are empty, and #validate does
134+
# not get this far with those (see ParamsScope#should_validate?). So
135+
# the checks around the attribute loop drop out. The machinery cost
136+
# more than the validation itself.
135137
def validate_attributes!(params)
136138
array_errors = nil
137139

‎lib/grape/validations/validators/default_validator.rb‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ def validate!(params)
3131
private
3232

3333
# Also #validate!'s whole job where Base#validate_attributes! applies:
34-
# a scope that is always validated and does not iterate elements, once
35-
# its params resolved to a Hash -- the root scope of every +params+
36-
# block with a default in it. A scope like that depends on no other
37-
# param, so the iterator and its dependency check have nothing to add.
34+
# a scope with no Array scope and no +given+ on its chain, once its
35+
# params resolved to a Hash -- the root scope of every +params+ block
36+
# with a default in it, and the Hash scopes under it. A scope like
37+
# that depends on no other param, so the iterator and its dependency
38+
# check have nothing to add.
3839
def apply_defaults(params)
3940
@attrs.each do |attr_name|
4041
params[attr_name] = @default_call.call(params) if params[attr_name].nil?

0 commit comments

Comments
 (0)