diff --git a/CHANGELOG.md b/CHANGELOG.md index f2a5b6ce5..fce53e03e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ * [#2594](https://github.com/ruby-grape/grape/pull/2594): Fix routes memoization - [@ericproulx](https://github.com/ericproulx). * [#2595](https://github.com/ruby-grape/grape/pull/2595): Keep `within_namespace` as part of our internal api - [@ericproulx](https://github.com/ericproulx). * [#2596](https://github.com/ruby-grape/grape/pull/2596): Remove `namespace_reverse_stackable_with_hash` from public scope - [@ericproulx](https://github.com/ericproulx). +* [#2621](https://github.com/ruby-grape/grape/pull/2621): Update upgrading notes regarding `return` usage and simplify endpoint execution - [@ericproulx](https://github.com/ericproulx). * Your contribution here. ### 2.4.0 (2025-06-18) diff --git a/UPGRADING.md b/UPGRADING.md index e84952924..1e6b1b991 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -23,7 +23,7 @@ See [#2617](https://github.com/ruby-grape/grape/pull/2617) for more information. #### Endpoint execution simplified and `return` deprecated -Executing a endpoint's block has been simplified and calling `return` in it has been deprecated. +Executing a endpoint's block has been simplified and calling `return` in it has been deprecated. Use `next` instead. See [#2577](https://github.com/ruby-grape/grape/pull/2577) for more information. diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index ecc0ea1fc..3c3a3a560 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -71,24 +71,10 @@ def initialize(new_settings, options = {}, &block) @lazy_initialize_lock = Mutex.new @lazy_initialized = nil - @block = nil - @status = nil @stream = nil @body = nil - - if block - @source = block - @block = lambda do |endpoint_instance| - ActiveSupport::Notifications.instrument('endpoint_render.grape', endpoint: endpoint_instance) do - endpoint_instance.instance_exec(&block) - rescue LocalJumpError => e - Grape.deprecator.warn 'Using `return` in an endpoint has been deprecated.' - return e.exit_value - end - end - end - + @source = block @helpers = build_helpers end @@ -255,7 +241,14 @@ def run end def execute - @block&.call(self) + 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 + end end def lazy_initialize! diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index f6da9365c..3f4d484ea 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -771,16 +771,32 @@ def memoized expect(last_response.body).to eq('yo') end - it 'allows explicit return calls' do - subject.get('/home') do - return 'Hello' + context 'when `return`' do + it 'calls deprecator' 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 - expect(Grape.deprecator).to receive(:warn).with('Using `return` in an endpoint has been deprecated.') + context 'when `next`' do + it 'does not call deprecator' do + subject.get('/home') do + next 'Hello' + end - get '/home' - expect(last_response.status).to eq(200) - expect(last_response.body).to eq('Hello') + expect(Grape.deprecator).not_to receive(:warn) + + get '/home' + expect(last_response.status).to eq(200) + expect(last_response.body).to eq('Hello') + end end context 'filters' do