Fix ArgumentError for single-argument lambdas in expose and conditions#399
Merged
Fix ArgumentError for single-argument lambdas in expose and conditions#399
Conversation
Use block.arity == 1 instead of block.parameters.one? so that
splat-arg lambdas (->(*args) { }) receive both object and options
instead of being misrouted to the single-arg path.
Remove dead return values from ensure_block_arity! since the caller
no longer uses its return value — it is now purely a validation method.
Cover single-argument blocks in expose_nil condition and callable :as option. Fix misleading test context names that said "lambda" for do...end blocks which are procs.
There was a problem hiding this comment.
Pull request overview
Restores compatibility with Ruby 3+ single-argument lambdas used in expose-related callables (including exposure blocks and if: / unless: conditions) that began raising ArgumentError in v1.0.2, while keeping the symbol-to-proc (&:method) arity validation introduced in #389.
Changes:
- Update
exec_with_objectto pass only the entity object to single-argument callables (and to symbol-to-proc wrappers after validation). - Adjust
ensure_block_arity!to act purely as a validator (no longer used as an arity-returning helper). - Add specs covering single-arg lambdas for
as:andif:conditions, plus block arity behavior; update CHANGELOG.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| spec/grape_entity/entity_spec.rb | Adds regression tests for single-argument lambdas and block arity handling in exposures/conditions. |
| lib/grape_entity/entity.rb | Fixes callable invocation arity to avoid passing options into single-arg lambdas; preserves &:method validation. |
| CHANGELOG.md | Documents the fix under Unreleased. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Upgrading to 1.0.2 breaks commonly used single-argument lambda patterns in
exposeblocks andif:conditions:These raise
ArgumentError: wrong number of arguments (given 2, expected 1)since v1.0.2.This PR restores support for single-argument lambdas while preserving the symbol-to-proc (
&:method) validation introduced in #389. Users who already applied the two-argument workaround (->(model, _) { ... }) are not affected — both styles work.Reported in #398.