From 334aabba8fc487bcbce60284a6e2502833cbb102 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 1 Sep 2024 13:44:19 +0200 Subject: [PATCH 1/5] Move inspect method to public Calls super if env is not defined Add spec --- lib/grape/endpoint.rb | 13 +++++++++---- spec/grape/endpoint_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 88605d13b8..e805a07244 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..009c67ae7c 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -1088,4 +1088,24 @@ def memoized ) end end + + context '#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 }.to_not raise_error + end + end end From dd5154e4c88dd95be952318bdddc70214d1a678f Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 1 Sep 2024 13:49:22 +0200 Subject: [PATCH 2/5] Add CHANGELOG.md entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1000c1a4c..8af3df96ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,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's inspect method - [@ericproulx](https://github.com/ericproulx). * Your contribution here. ### 2.1.3 (2024-07-13) From 4f6a48daa11e107d18811f122960f7c20057c43e Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 1 Sep 2024 13:57:00 +0200 Subject: [PATCH 3/5] Fix rubocop --- spec/grape/endpoint_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index 009c67ae7c..dd25b814b0 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -1089,7 +1089,7 @@ def memoized end end - context '#inspect' do + describe '#inspect' do subject { described_class.new(settings, options).inspect } let(:options) do @@ -1105,7 +1105,7 @@ def memoized let(:settings) { Grape::Util::InheritableSetting.new } it 'does not raise an error' do - expect { subject }.to_not raise_error + expect { subject }.not_to raise_error end end end From e0c1c0ad0c7a9958add5073e76fe0f35a6a33a7f Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 1 Sep 2024 19:15:33 +0200 Subject: [PATCH 4/5] Fix comments --- CHANGELOG.md | 2 +- lib/grape/endpoint.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff3c7a0fdc..2eda07b1b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +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's inspect method - [@ericproulx](https://github.com/ericproulx). +* [#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 e805a07244..9072ba877c 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -234,13 +234,13 @@ 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 + # 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" + "#{self.class} in '#{route.origin}' endpoint" end protected From 1067f37430281fa7d9d08f42386783f92b3f43a2 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 1 Sep 2024 19:36:28 +0200 Subject: [PATCH 5/5] Change backtick for single quote in test --- spec/grape/endpoint_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index dd25b814b0..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