Skip to content
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 @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
25 changes: 9 additions & 16 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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!
Expand Down
30 changes: 23 additions & 7 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down