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

Replace method_missing by an overrided inspect #2444

Merged
merged 2 commits into from
May 20, 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 @@ -42,6 +42,7 @@
* [#2405](https://github.com/ruby-grape/grape/pull/2405): Fix edge workflow - [@ericproulx](https://github.com/ericproulx).
* [#2414](https://github.com/ruby-grape/grape/pull/2414): Fix Rack::Lint missing content-type - [@ericproulx](https://github.com/ericproulx).
* [#2378](https://github.com/ruby-grape/grape/pull/2378): Do not overwrite `route_param` with a regular one if they share same name - [@arg](https://github.com/arg).
* [#2444](https://github.com/ruby-grape/grape/pull/2444): Replace method_missing in endpoint - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

### 2.0.0 (2023/11/11)
Expand Down
8 changes: 2 additions & 6 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,8 @@ def options?
env[Rack::REQUEST_METHOD] == Rack::OPTIONS
end

def method_missing(name, *_args)
raise NoMethodError.new("undefined method `#{name}' for #{self.class} in `#{route.origin}' endpoint")
end

def respond_to_missing?(method_name, include_private = false)
super
def inspect
"#{self.class} in `#{route.origin}' endpoint"
end
end
end
20 changes: 12 additions & 8 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -679,15 +679,19 @@ def app
end
end

describe '#method_missing' do
context 'when referencing an undefined local variable' do
it 'raises NoMethodError but stripping the internals of the Grape::Endpoint class and including the API route' do
subject.get('/hey') do
undefined_helper
describe 'NameError' do
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}
else
/undefined local variable or method `undefined_helper' for/
end
expect do
get '/hey'
end.to raise_error(NoMethodError, %r{^undefined method `undefined_helper' for #<Class:0x[0-9a-fA-F]+> in `/hey' endpoint})
end

it 'raises NameError but stripping the internals of the Grape::Endpoint class and including the API route' do
subject.get('/hey') { undefined_helper }
expect { get '/hey' }.to raise_error(NameError, error_message)
end
end
end
Expand Down