diff options
author | Jeremy Kemper <jeremykemper@gmail.com> | 2014-09-14 00:58:45 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremykemper@gmail.com> | 2014-09-14 14:11:26 -0700 |
commit | e3a65c6d7ca1f232dcd70359e76b7d5ebc08fe2f (patch) | |
tree | 21626876b924cc16f04037798bc13729bc0dc08a /activejob/lib/active_job/arguments.rb | |
parent | 01ac23423d2d7a0b3461ecfc6061702a413b4d96 (diff) | |
download | rails-e3a65c6d7ca1f232dcd70359e76b7d5ebc08fe2f.tar.gz rails-e3a65c6d7ca1f232dcd70359e76b7d5ebc08fe2f.tar.bz2 rails-e3a65c6d7ca1f232dcd70359e76b7d5ebc08fe2f.zip |
Tighten up AJ::Arguments and its tests
* Disallow deserialization of non-primitive objects
* Broaden coverage; remove superfluous tests
Diffstat (limited to 'activejob/lib/active_job/arguments.rb')
-rw-r--r-- | activejob/lib/active_job/arguments.rb | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/activejob/lib/active_job/arguments.rb b/activejob/lib/active_job/arguments.rb index 175a2f0956..099ea4d169 100644 --- a/activejob/lib/active_job/arguments.rb +++ b/activejob/lib/active_job/arguments.rb @@ -37,14 +37,16 @@ module ActiveJob private def serialize_argument(argument) case argument - when GlobalID::Identification - argument.to_global_id.to_s when *TYPE_WHITELIST argument + when GlobalID::Identification + argument.to_global_id.to_s when Array argument.map { |arg| serialize_argument(arg) } when Hash - Hash[ argument.map { |key, value| [ serialize_hash_key(key), serialize_argument(value) ] } ] + argument.each_with_object({}) do |(key, value), hash| + hash[serialize_hash_key(key)] = serialize_argument(value) + end else raise SerializationError.new("Unsupported argument type: #{argument.class.name}") end @@ -52,14 +54,18 @@ module ActiveJob def deserialize_argument(argument) case argument + when String + GlobalID::Locator.locate(argument) || argument + when *TYPE_WHITELIST + argument when Array argument.map { |arg| deserialize_argument(arg) } when Hash - Hash[ argument.map { |key, value| [ key, deserialize_argument(value) ] } ].with_indifferent_access - when String, GlobalID - GlobalID::Locator.locate(argument) || argument + argument.each_with_object({}.with_indifferent_access) do |(key, value), hash| + hash[key] = deserialize_argument(value) + end else - argument + raise ArgumentError, "Can only deserialize primitive arguments: #{argument.inspect}" end end @@ -68,7 +74,7 @@ module ActiveJob when String, Symbol key.to_s else - raise SerializationError.new("Unsupported hash key type: #{key.class.name}") + raise SerializationError.new("Only string and symbol hash keys may be serialized as job arguments, but #{key.inspect} is a #{key.class}") end end end |