Skip to content

Commit

Permalink
Fix Grape::Endpoint's inspect method when not called in the context o…
Browse files Browse the repository at this point in the history
…f 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
  • Loading branch information
ericproulx authored Sep 1, 2024
1 parent 41adcb7 commit 1cf4a80
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
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

0 comments on commit 1cf4a80

Please sign in to comment.