File tree Expand file tree Collapse file tree 3 files changed +22
-2
lines changed
lib/active_job/serializers Expand file tree Collapse file tree 3 files changed +22
-2
lines changed Original file line number Diff line number Diff line change
1
+ * Fix deserialization of ActiveSupport::Duration
2
+
3
+ Previously, a deserialized Duration would return an array from Duration#parts.
4
+ It will now return a hash just like a regular Duration.
5
+
6
+ This also fixes an error when trying to add or subtract from a deserialized Duration
7
+ (eg ` duration + 1.year ` ).
8
+
9
+ * Jonathan del Strother*
10
+
1
11
* ` perform_enqueued_jobs ` is now compatible with all Active Job adapters
2
12
3
13
This means that methods that depend on it, like Action Mailer's ` assert_emails ` ,
Original file line number Diff line number Diff line change @@ -4,14 +4,16 @@ module ActiveJob
4
4
module Serializers
5
5
class DurationSerializer < ObjectSerializer # :nodoc:
6
6
def serialize ( duration )
7
+ # Ideally duration.parts would be wrapped in an array before passing to Arguments.serialize,
8
+ # but we continue passing the bare hash for backwards compatibility:
7
9
super ( "value" => duration . value , "parts" => Arguments . serialize ( duration . parts ) )
8
10
end
9
11
10
12
def deserialize ( hash )
11
13
value = hash [ "value" ]
12
14
parts = Arguments . deserialize ( hash [ "parts" ] )
13
-
14
- klass . new ( value , parts )
15
+ # `parts` is originally a hash, but will have been flattened to an array by Arguments.serialize
16
+ klass . new ( value , parts . to_h )
15
17
end
16
18
17
19
private
Original file line number Diff line number Diff line change 5
5
require "active_job/arguments"
6
6
require "models/person"
7
7
require "active_support/core_ext/hash/indifferent_access"
8
+ require "active_support/core_ext/integer/time"
9
+ require "active_support/duration"
8
10
require "jobs/kwargs_job"
9
11
require "jobs/arguments_round_trip_job"
10
12
require "support/stubs/strong_parameters"
@@ -188,6 +190,12 @@ def self.permitted?
188
190
end
189
191
end
190
192
193
+ test "should maintain a functional duration" do
194
+ duration = perform_round_trip ( [ 1 . year ] ) . first
195
+ assert_kind_of Hash , duration . parts
196
+ assert_equal 2 . years , duration + 1 . year
197
+ end
198
+
191
199
test "should disallow non-string/symbol hash keys" do
192
200
assert_raises ActiveJob ::SerializationError do
193
201
ActiveJob ::Arguments . serialize [ { 1 => 2 } ]
You can’t perform that action at this time.
0 commit comments