Skip to content

Fix ArgumentError for single-argument lambdas in expose and conditions#399

Merged
numbata merged 4 commits intomasterfrom
fix/lambda-arity-regression
Apr 15, 2026
Merged

Fix ArgumentError for single-argument lambdas in expose and conditions#399
numbata merged 4 commits intomasterfrom
fix/lambda-arity-regression

Conversation

@numbata
Copy link
Copy Markdown
Collaborator

@numbata numbata commented Apr 15, 2026

Summary

Upgrading to 1.0.2 breaks commonly used single-argument lambda patterns in expose blocks and if: conditions:

expose :property, if: ->(model) { model.active? }

expose :storage, if: ->(model) { model.storage_method.present? } do |model|
  check_storage(model, :storage_method)
end

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.

numbata added 3 commits April 15, 2026 14:23
PR #389 changed arity detection to use block.arity.zero? which broke
single-argument lambdas in expose blocks and if: conditions, causing
ArgumentError (given 2, expected 1). Restore parameter count check
for regular blocks while keeping symbol-to-proc detection intact.

Fixes #398
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.
@numbata numbata changed the title Fix/lambda arity regression Fix ArgumentError for single-argument lambdas in expose and conditions Apr 15, 2026
@numbata numbata marked this pull request as ready for review April 15, 2026 12:47
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_object to 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: and if: 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.

@numbata numbata merged commit 2ec5246 into master Apr 15, 2026
10 checks passed
@numbata numbata self-assigned this Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants