Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Grape::Endpoint's inspect method when not called in the context of an API #2492

Merged
merged 6 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [#2480](https://github.com/ruby-grape/grape/pull/2480): Fix rescue_from ValidationErrors exception - [@numbata](https://github.com/numbata).
* [#2464](https://github.com/ruby-grape/grape/pull/2464): The `length` validator only takes effect for parameters with types that support `#length` method - [@OuYangJinTing](https://github.com/OuYangJinTing).
* [#2485](https://github.com/ruby-grape/grape/pull/2485): Add `is:` param to length validator - [@dakad](https://github.com/dakad).
* [#2492](https://github.com/ruby-grape/grape/pull/2492): Fix `Grape::Endpoint#inspect` method - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

### 2.1.3 (2024-07-13)
Expand Down
13 changes: 9 additions & 4 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ def equals?(endpoint)
(options == endpoint.options) && (inheritable_setting.to_hash == endpoint.inheritable_setting.to_hash)
end

# The purpose of this override is solely for stripping internals when an error occurs while calling
# an endpoint through an api. See https://github.com/ruby-grape/grape/issues/2398
# Otherwise, it calls super.
def inspect
return super unless env

"#{self.class} in '#{route.origin}' endpoint"
end

protected

def run
Expand Down Expand Up @@ -403,9 +412,5 @@ def options?
options[:options_route_enabled] &&
env[Rack::REQUEST_METHOD] == Rack::OPTIONS
end

def inspect
"#{self.class} in `#{route.origin}' endpoint"
end
end
end
22 changes: 21 additions & 1 deletion spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ def app
context 'when referencing an undefined local variable or method' do
let(:error_message) do
if Gem::Version.new(RUBY_VERSION).release <= Gem::Version.new('3.2')
%r{undefined local variable or method `undefined_helper' for #<Class:0x[0-9a-fA-F]+> in `/hey' endpoint}
%r{undefined local variable or method `undefined_helper' for #<Class:0x[0-9a-fA-F]+> in '/hey' endpoint}
else
/undefined local variable or method `undefined_helper' for/
end
Expand Down Expand Up @@ -1088,4 +1088,24 @@ def memoized
)
end
end

describe '#inspect' do
subject { described_class.new(settings, options).inspect }

let(:options) do
{
method: :path,
path: '/path',
app: {},
route_options: { anchor: false },
forward_match: true,
for: Class.new
}
end
let(:settings) { Grape::Util::InheritableSetting.new }

it 'does not raise an error' do
expect { subject }.not_to raise_error
end
end
end