Skip to content

Commit 7d1c7e1

Browse files
author
Tim Perkins
committed
Do not modify a Hash argument to error!
This change allows an immutable hash, for example a frozen constant, to be passed as the message to `error!`. If the message is a Hash, then it is always duplicated, and any `:with` key is removed from the copy.
1 parent dc77375 commit 7d1c7e1

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [#1325](https://github.com/ruby-grape/grape/pull/1325): Params: Fix coerce_with helper with Array types - [@ngonzalez](https://github.com/ngonzalez).
1313
* [#1326](https://github.com/ruby-grape/grape/pull/1326): Fix wrong behavior for OPTIONS and HEAD requests with catch-all - [@ekampp](https://github.com/ekampp), [@namusyaka](https://github.com/namusyaka).
1414
* [#1330](https://github.com/ruby-grape/grape/pull/1330): Add `register` keyword for adding customized parsers and formatters - [@namusyaka](https://github.com/namusyaka).
15+
* [#1336](https://github.com/ruby-grape/grape/pull/1336): Do not modify Hash argument to `error!` - [@tjwp](https://github.com/tjwp).
1516

1617
0.15.0 (3/8/2016)
1718
=================

lib/grape/error_formatter/base.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ module ErrorFormatter
33
module Base
44
def present(message, env)
55
present_options = {}
6-
present_options[:with] = message.delete(:with) if message.is_a?(Hash)
6+
presented_message = message
7+
if presented_message.is_a?(Hash)
8+
presented_message = presented_message.dup
9+
present_options[:with] = presented_message.delete(:with)
10+
end
711

8-
presenter = env[Grape::Env::API_ENDPOINT].entity_class_for_obj(message, present_options)
12+
presenter = env[Grape::Env::API_ENDPOINT].entity_class_for_obj(presented_message, present_options)
913

1014
unless presenter || env[Grape::Env::GRAPE_ROUTING_ARGS].nil?
1115
# env['api.endpoint'].route does not work when the error occurs within a middleware
@@ -21,10 +25,10 @@ def present(message, env)
2125
if presenter
2226
embeds = { env: env }
2327
embeds[:version] = env[Grape::Env::API_VERSION] if env[Grape::Env::API_VERSION]
24-
message = presenter.represent(message, embeds).serializable_hash
28+
presented_message = presenter.represent(presented_message, embeds).serializable_hash
2529
end
2630

27-
message
31+
presented_message
2832
end
2933
end
3034
end

spec/grape/api_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,7 @@ def static
20462046
end
20472047

20482048
it 'presented with' do
2049-
error = { code: 408, with: error_presenter }
2049+
error = { code: 408, with: error_presenter }.freeze
20502050
subject.get '/exception' do
20512051
error! error, 408
20522052
end

spec/grape/endpoint_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,16 @@ def app
742742
expect(last_response.body).to eq('{"dude":"rad"}')
743743
end
744744

745+
it 'accepts a frozen object' do
746+
subject.get '/hey' do
747+
error!({ 'dude' => 'rad' }.freeze, 403)
748+
end
749+
750+
get '/hey.json'
751+
expect(last_response.status).to eq(403)
752+
expect(last_response.body).to eq('{"dude":"rad"}')
753+
end
754+
745755
it 'can specifiy headers' do
746756
subject.get '/hey' do
747757
error!({ 'dude' => 'rad' }, 403, 'X-Custom' => 'value')

0 commit comments

Comments
 (0)