Skip to content

Commit cc82601

Browse files
ericproulxdblock
andauthored
Update UPGRADING notes regarding return usage and simplify endpoint execution (#2621)
* Update UPGRADING notes regarding `return` usage and simplify block calling in endpoint. * Update spec/grape/endpoint_spec.rb Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]> * Update spec/grape/endpoint_spec.rb Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]> --------- Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
1 parent a28218b commit cc82601

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* [#2594](https://github.com/ruby-grape/grape/pull/2594): Fix routes memoization - [@ericproulx](https://github.com/ericproulx).
3535
* [#2595](https://github.com/ruby-grape/grape/pull/2595): Keep `within_namespace` as part of our internal api - [@ericproulx](https://github.com/ericproulx).
3636
* [#2596](https://github.com/ruby-grape/grape/pull/2596): Remove `namespace_reverse_stackable_with_hash` from public scope - [@ericproulx](https://github.com/ericproulx).
37+
* [#2621](https://github.com/ruby-grape/grape/pull/2621): Update upgrading notes regarding `return` usage and simplify endpoint execution - [@ericproulx](https://github.com/ericproulx).
3738
* Your contribution here.
3839

3940
### 2.4.0 (2025-06-18)

UPGRADING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ See [#2617](https://github.com/ruby-grape/grape/pull/2617) for more information.
2323

2424
#### Endpoint execution simplified and `return` deprecated
2525

26-
Executing a endpoint's block has been simplified and calling `return` in it has been deprecated.
26+
Executing a endpoint's block has been simplified and calling `return` in it has been deprecated. Use `next` instead.
2727

2828
See [#2577](https://github.com/ruby-grape/grape/pull/2577) for more information.
2929

lib/grape/endpoint.rb

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,10 @@ def initialize(new_settings, options = {}, &block)
7171

7272
@lazy_initialize_lock = Mutex.new
7373
@lazy_initialized = nil
74-
@block = nil
75-
7674
@status = nil
7775
@stream = nil
7876
@body = nil
79-
80-
if block
81-
@source = block
82-
@block = lambda do |endpoint_instance|
83-
ActiveSupport::Notifications.instrument('endpoint_render.grape', endpoint: endpoint_instance) do
84-
endpoint_instance.instance_exec(&block)
85-
rescue LocalJumpError => e
86-
Grape.deprecator.warn 'Using `return` in an endpoint has been deprecated.'
87-
return e.exit_value
88-
end
89-
end
90-
end
91-
77+
@source = block
9278
@helpers = build_helpers
9379
end
9480

@@ -255,7 +241,14 @@ def run
255241
end
256242

257243
def execute
258-
@block&.call(self)
244+
return unless @source
245+
246+
ActiveSupport::Notifications.instrument('endpoint_render.grape', endpoint: self) do
247+
instance_exec(&@source)
248+
rescue LocalJumpError => e
249+
Grape.deprecator.warn 'Using `return` in an endpoint has been deprecated. Use `next` instead.'
250+
return e.exit_value
251+
end
259252
end
260253

261254
def lazy_initialize!

spec/grape/endpoint_spec.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -771,16 +771,32 @@ def memoized
771771
expect(last_response.body).to eq('yo')
772772
end
773773

774-
it 'allows explicit return calls' do
775-
subject.get('/home') do
776-
return 'Hello'
774+
context 'when `return`' do
775+
it 'calls deprecator' do
776+
subject.get('/home') do
777+
return 'Hello'
778+
end
779+
780+
expect(Grape.deprecator).to receive(:warn).with('Using `return` in an endpoint has been deprecated. Use `next` instead.')
781+
782+
get '/home'
783+
expect(last_response.status).to eq(200)
784+
expect(last_response.body).to eq('Hello')
777785
end
786+
end
778787

779-
expect(Grape.deprecator).to receive(:warn).with('Using `return` in an endpoint has been deprecated.')
788+
context 'when `next`' do
789+
it 'does not call deprecator' do
790+
subject.get('/home') do
791+
next 'Hello'
792+
end
780793

781-
get '/home'
782-
expect(last_response.status).to eq(200)
783-
expect(last_response.body).to eq('Hello')
794+
expect(Grape.deprecator).not_to receive(:warn)
795+
796+
get '/home'
797+
expect(last_response.status).to eq(200)
798+
expect(last_response.body).to eq('Hello')
799+
end
784800
end
785801

786802
context 'filters' do

0 commit comments

Comments
 (0)