Commit 4d71e0e
Block arity detection (#389)
* 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]>1 parent 489f9b9 commit 4d71e0e
2 files changed
+84
-27
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
519 | 519 | | |
520 | 520 | | |
521 | 521 | | |
522 | | - | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
523 | 529 | | |
524 | 530 | | |
525 | 531 | | |
526 | 532 | | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
532 | 545 | | |
533 | 546 | | |
534 | | - | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
535 | 563 | | |
536 | 564 | | |
537 | 565 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
393 | 393 | | |
394 | 394 | | |
395 | 395 | | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
396 | 404 | | |
397 | 405 | | |
398 | 406 | | |
| |||
423 | 431 | | |
424 | 432 | | |
425 | 433 | | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
442 | 437 | | |
443 | | - | |
| 438 | + | |
444 | 439 | | |
445 | | - | |
446 | | - | |
447 | | - | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
448 | 477 | | |
449 | 478 | | |
450 | 479 | | |
| |||
0 commit comments