You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Fix the method Grape::Entity#exec_with_object to work with Ruby 3.
* Fix(Entity): Improve block arity detection for `Symbol#to_proc`
Addresses `ArgumentError`s when using `&:method_name` with `expose`,
a scenario particularly affected by Ruby 3.0's stricter argument handling
for procs. The previous arity check, including the condition
`block.parameters == [[:req], [:rest]]`, was not consistently reliable
for these cases.
This change introduces `determine_block_arity` which:
1. Attempts to parse the original method name from `Proc#to_s` for
blocks likely created via `&:method_name`.
2. If the method name is found and the object responds to it, this
logic uses the *actual arity of the original method* to correctly
determine how `instance_exec` should be called.
3. If the original method name can't be determined, it falls back
to using `block.parameters.size`.
This ensures methods exposed via `&:method_name` are called with the
correct arguments (i.e., with or without `options`), resolving the
`ArgumentError`s and removing the need for the previous `rescue` logic.
* Fix rubocop offense
* Fix(Entity): Enforce zero-arity for Symbol#to_proc in exec_with_object
Ruby 3.0’s stricter arity rules meant `&:method_name` blocks could be called with wrong args,
causing errors. The old check (`parameters == [[:req],[:rest]]` + `parameters.size`) was unreliable.
This update:
- Adds `symbol_to_proc_wrapper?` to detect pure `&:method_name` Procs (checks `lambda?`,
`source_location.nil?`, and `parameters == [[:req],[:rest]]`).
- Introduces `determine_block_arity`, which parses the method name from `block.to_s`; if
the object responds to it, it uses `object.method(name).arity`, otherwise falls back
to `block.arity`.
- In `exec_with_object`, symbol-to-proc wrappers are required to have zero arity (raising
`ArgumentError` otherwise), while regular Procs use `block.arity` to decide between
`instance_exec(object)` and `instance_exec(object, options)`.
This removes rescue logic and ensures `&:method_name` is only used for zero-argument methods.
* Correct error message
* Fix(Entity): Validate `&:method_name` arity and method presence
Ensure symbol-to-proc exposures only work for zero-argument methods:
- Raise if the method is undefined on the object
- Raise if the method expects one or more arguments
- Fall back to `Proc#arity` for regular blocks
---------
Co-authored-by: Chau Hong Linh <[email protected]>
0 commit comments