Skip to content

Commit 1cf4a80

Browse files
authored
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
1 parent 41adcb7 commit 1cf4a80

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [#2480](https://github.com/ruby-grape/grape/pull/2480): Fix rescue_from ValidationErrors exception - [@numbata](https://github.com/numbata).
1616
* [#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).
1717
* [#2485](https://github.com/ruby-grape/grape/pull/2485): Add `is:` param to length validator - [@dakad](https://github.com/dakad).
18+
* [#2492](https://github.com/ruby-grape/grape/pull/2492): Fix `Grape::Endpoint#inspect` method - [@ericproulx](https://github.com/ericproulx).
1819
* Your contribution here.
1920

2021
### 2.1.3 (2024-07-13)

lib/grape/endpoint.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ def equals?(endpoint)
234234
(options == endpoint.options) && (inheritable_setting.to_hash == endpoint.inheritable_setting.to_hash)
235235
end
236236

237+
# The purpose of this override is solely for stripping internals when an error occurs while calling
238+
# an endpoint through an api. See https://github.com/ruby-grape/grape/issues/2398
239+
# Otherwise, it calls super.
240+
def inspect
241+
return super unless env
242+
243+
"#{self.class} in '#{route.origin}' endpoint"
244+
end
245+
237246
protected
238247

239248
def run
@@ -403,9 +412,5 @@ def options?
403412
options[:options_route_enabled] &&
404413
env[Rack::REQUEST_METHOD] == Rack::OPTIONS
405414
end
406-
407-
def inspect
408-
"#{self.class} in `#{route.origin}' endpoint"
409-
end
410415
end
411416
end

spec/grape/endpoint_spec.rb

+21-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ def app
683683
context 'when referencing an undefined local variable or method' do
684684
let(:error_message) do
685685
if Gem::Version.new(RUBY_VERSION).release <= Gem::Version.new('3.2')
686-
%r{undefined local variable or method `undefined_helper' for #<Class:0x[0-9a-fA-F]+> in `/hey' endpoint}
686+
%r{undefined local variable or method `undefined_helper' for #<Class:0x[0-9a-fA-F]+> in '/hey' endpoint}
687687
else
688688
/undefined local variable or method `undefined_helper' for/
689689
end
@@ -1088,4 +1088,24 @@ def memoized
10881088
)
10891089
end
10901090
end
1091+
1092+
describe '#inspect' do
1093+
subject { described_class.new(settings, options).inspect }
1094+
1095+
let(:options) do
1096+
{
1097+
method: :path,
1098+
path: '/path',
1099+
app: {},
1100+
route_options: { anchor: false },
1101+
forward_match: true,
1102+
for: Class.new
1103+
}
1104+
end
1105+
let(:settings) { Grape::Util::InheritableSetting.new }
1106+
1107+
it 'does not raise an error' do
1108+
expect { subject }.not_to raise_error
1109+
end
1110+
end
10911111
end

0 commit comments

Comments
 (0)