Hoist using: / except: to explicit kwargs on requires / optional#2746
Open
ericproulx wants to merge 1 commit into
Open
Hoist using: / except: to explicit kwargs on requires / optional#2746ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
`Grape::DSL::Parameters#requires` and `#optional` both treat `:using` (and the `:except` it pairs with) as a method-local early-return signal: when `:using` is set, the method returns through `require_required_and_optional_fields` / `require_optional_fields` and the keys never flow into `validate_attributes` or the validator/scope machinery. Promote them out of the `**opts` Hash into explicit kwargs: def requires(*attrs, using: nil, except: nil, **opts, &block) def optional(*attrs, using: nil, except: nil, **opts, &block) The signature now self-documents the using/except contract instead of burying it in opts, the `if using` guard drops one Hash lookup, and the `opts` Hash that flows into `validate_attributes` is "validator-bound kwargs only" — easier to reason about what propagates downstream. Fully back-compat: Ruby kwarg matching captures `using:` / `except:` before the `**opts` splat, so existing call sites (`requires :all, using: docs`, `optional :all, except: %i[…], using: docs`, etc.) keep working unchanged. The unrelated `:except_values` validator option (consumed by `Grape::Validations::ValidationsSpec`) is a different key and is unaffected. Matches the recent kwargs-hoisting pattern in #2723 (`desc`) and #2728 (`auth` / `http_basic` / `http_digest`). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
4343f71 to
cbf759f
Compare
Danger ReportNo issues found. |
dblock
approved these changes
May 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Grape::DSL::Parameters#requiresand#optionalboth treat:using(and the:exceptit pairs with) as a method-local early-return signal — when:usingis set, the method returns throughrequire_required_and_optional_fields/require_optional_fieldsand the keys never flow intovalidate_attributesor the validator/scope machinery.Promote them out of the
**optsHash into explicit kwargs:The signature now self-documents the using/except contract instead of burying it in opts; the
if usingguard drops one Hash lookup; and theoptsHash that flows intovalidate_attributesis "validator-bound kwargs only" — easier to reason about what propagates downstream.Backward compatibility
Fully back-compat: Ruby kwarg matching captures
using:/except:before the**optssplat, so existing call sites (requires :all, using: docs,optional :all, except: %i[…], using: docs, etc.) keep working unchanged.The unrelated
:except_valuesvalidator option (consumed byGrape::Validations::ValidationsSpec) is a different key and is unaffected.Test plan
bundle exec rspec— 2313 examples, 0 failuresbundle exec rubocop lib/grape/dsl/parameters.rb— cleanMatches the recent kwargs-hoisting pattern in #2723 (
desc) and #2728 (auth/http_basic/http_digest).🤖 Generated with Claude Code