Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoize Virtus attribute and fix memory leak. #1109

Merged
merged 1 commit into from
Aug 18, 2015
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
0.13.1 (Next)
=============

#### Features

* Your contribution here.

#### Fixes

* [#1109](https://github.com/ruby-grape/grape/pull/1109): Memoize Virtus attribute and fix memory leak - [@marshall-lee](https://github.com/marshall-lee).
* Your contribution here.

0.13.0 (8/10/2015)
Expand Down
29 changes: 19 additions & 10 deletions lib/grape/validations/validators/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Validations
class CoerceValidator < Base
def validate_param!(attr_name, params)
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :coerce unless params.is_a? Hash
new_value = coerce_value(@option, params[attr_name])
new_value = coerce_value(params[attr_name])
if valid_type?(new_value)
params[attr_name] = new_value
else
Expand Down Expand Up @@ -50,27 +50,36 @@ def valid_type?(val)
end
end

def coerce_value(type, val)
def coerce_value(val)
# Don't coerce things other than nil to Arrays or Hashes
return val || [] if type == Array
return val || Set.new if type == Set
return val || {} if type == Hash

# To support custom types that Virtus can't easily coerce, pass in an
# explicit coercer. Custom types must implement a `parse` class method.
converter_options = {}
if ParameterTypes.custom_type?(type)
converter_options[:coercer] = type.method(:parse)
end

converter = Virtus::Attribute.build(type, converter_options)
converter.coerce(val)

# not the prettiest but some invalid coercion can currently trigger
# errors in Virtus (see coerce_spec.rb:75)
rescue
InvalidValue.new
end

def type
@option
end

def converter
@converter ||=
begin
# To support custom types that Virtus can't easily coerce, pass in an
# explicit coercer. Custom types must implement a `parse` class method.
converter_options = {}
if ParameterTypes.custom_type?(type)
converter_options[:coercer] = type.method(:parse)
end
Virtus::Attribute.build(type, converter_options)
end
end
end
end
end
13 changes: 13 additions & 0 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,18 @@ class User
expect(last_response.body).to eq('Fixnum')
end
end

context 'converter' do
it 'does not build Virtus::Attribute multiple times' do
subject.params do
requires :something, type: Array[String]
end
subject.get do
end

expect(Virtus::Attribute).to receive(:build).at_most(2).times.and_call_original
10.times { get '/' }
end
end
end
end