From 1cf4a801efc22e65c4fb342ead57fe3c428a4b8d Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sun, 1 Sep 2024 19:56:08 +0200 Subject: [PATCH] Fix Grape::Endpoint's inspect method when not called in the context of an API (#2492) * Move inspect method to public Calls super if env is not defined Add spec * Add CHANGELOG.md entry * Fix rubocop * Fix comments * Change backtick for single quote in test --- CHANGELOG.md | 1 + lib/grape/endpoint.rb | 13 +++++++++---- spec/grape/endpoint_spec.rb | 22 +++++++++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16182f6356..2eda07b1b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 88605d13b8..9072ba877c 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -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 @@ -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 diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index ee44efc39a..f51e582138 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -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 # in `/hey' endpoint} + %r{undefined local variable or method `undefined_helper' for # in '/hey' endpoint} else /undefined local variable or method `undefined_helper' for/ end @@ -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