From 0ac4b327bc313757c958bee10729c0b9aa96407d Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sat, 13 Dec 2025 23:10:51 +0100 Subject: [PATCH] Revert PR #2577: Restore support for in endpoint blocks --- CHANGELOG.md | 1 + lib/grape/endpoint.rb | 16 +++++++++++----- spec/grape/endpoint_spec.rb | 20 ++------------------ 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cdc6880a9..c4e6cce5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ #### Fixes * [#2633](https://github.com/ruby-grape/grape/pull/2633): Fix cascade reading - [@ericproulx](https://github.com/ericproulx). +* [#2641](https://github.com/ruby-grape/grape/pull/2641): Restore support for `return` in endpoint blocks - [@ericproulx](https://github.com/ericproulx). * [#2642](https://github.com/ruby-grape/grape/pull/2642): Fix array allocation in base_route.rb - [@ericproulx](https://github.com/ericproulx). * Your contribution here. diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 24307185c..873f4b4db 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -32,6 +32,15 @@ def run_before_each(endpoint) superclass.run_before_each(endpoint) unless self == Endpoint before_each.each { |blk| blk.try(:call, endpoint) } end + + def block_to_unbound_method(block) + return unless block + + define_method :temp_unbound_method, block + method = instance_method(:temp_unbound_method) + remove_method :temp_unbound_method + method + end end # Create a new endpoint. @@ -70,7 +79,7 @@ def initialize(new_settings, **options, &block) @status = nil @stream = nil @body = nil - @source = block + @source = self.class.block_to_unbound_method(block) @before_filter_passed = false end @@ -190,10 +199,7 @@ def execute return unless @source ActiveSupport::Notifications.instrument('endpoint_render.grape', endpoint: self) do - instance_exec(&@source) - rescue LocalJumpError => e - Grape.deprecator.warn 'Using `return` in an endpoint has been deprecated. Use `next` instead.' - return e.exit_value + @source.bind_call(self) end end diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index 8ef338dd4..b82672aa1 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -762,28 +762,12 @@ def memoized expect(last_response.body).to eq('yo') end - context 'when `return`' do - it 'calls deprecator' do + context 'when calling return' do + it 'does not raise a LocalJumpError' do subject.get('/home') do return 'Hello' end - expect(Grape.deprecator).to receive(:warn).with('Using `return` in an endpoint has been deprecated. Use `next` instead.') - - get '/home' - expect(last_response.status).to eq(200) - expect(last_response.body).to eq('Hello') - end - end - - context 'when `next`' do - it 'does not call deprecator' do - subject.get('/home') do - next 'Hello' - end - - expect(Grape.deprecator).not_to receive(:warn) - get '/home' expect(last_response.status).to eq(200) expect(last_response.body).to eq('Hello')