Restore the nil-base fallback in Grape::API::Instance#to_s - #2833
Merged
Conversation
def_delegators :@base, :to_s (#2581) evaluates nil.to_s and returns "" for any Instance whose @base is unset -- notably the bare Grape::API::Instance class itself, which is never mounted and never has base= called on it, so it stays base-less forever. Anything that enumerates loaded classes and renders them by string (Sorbet/Tapioca RBI generation, log/error interpolation) sees an empty name instead of "Grape::API::Instance", while #name stays correct -- the disagreement is what makes it surprising. Replace the delegator with an explicit method that falls back to Module#to_s via super when @base is nil, matching the pre-3.0 behavior. Fixes #2832. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Danger ReportNo issues found. |
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
Fixes #2832.
Grape::API::Instance.to_sreturns""instead of its class name.#nameis still correct — only#to_sis blank, which is what makes it surprising.Cause
PR #2581 replaced an explicit, nil-guarded
to_soverride with:@baseis only assigned viabase=, which runs whenGrape::API.mount_instancebuilds a mount instance (lib/grape/api.rb). The bareGrape::API::Instanceclass is never mounted and never hasbase=called on it, so it stays@base-less forever — not a transient state, but permanent for that specific class. The delegator then evaluatesnil.to_s, yielding"".Anything that enumerates loaded classes and renders them by string representation (Sorbet/Tapioca RBI generation, log/error message interpolation) sees an empty name instead of
Grape::API::Instance.Fix
def_delegators :@base, :to_s(viaextend Forwardable)superresolves through the singleton-class ancestry toModule#to_s, restoring the pre-3.0.0 behavior. The@baseset case is unaffected — empty string is truthy in Ruby, so the delegation branch behaves exactly as before once an instance is mounted.extend Forwardableis dropped fromGrape::API::Instance's singleton class since it existed solely for this one delegator.Notes
describe '.to_s'coverage inspec/grape/api/instance_spec.rb: one example for the unmounted fallback, one mirroring the issue's repro (SomeAPI.base_instance.to_s == 'SomeAPI') for the mounted case.bundle exec rubocopandbundle exec rspec(2515 examples) pass clean.🤖 Generated with Claude Code