Skip to content

Commit 0e62756

Browse files
authored
Merge pull request #56093 from skipkayhil/hm-rynrvruzqlkttuqt
Fix Arguments#serialize missing custom serializers
2 parents 9b054c6 + c0dc92e commit 0e62756

File tree

7 files changed

+33
-28
lines changed

7 files changed

+33
-28
lines changed

activejob/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
* Fix using custom serializers with `ActiveJob::Arguments.serialize` when
2+
`ActiveJob::Base` hasn't been loaded.
3+
4+
*Hartley McGuire*
15

26
Please check [8-1-stable](https://github.com/rails/rails/blob/8-1-stable/activejob/CHANGELOG.md) for previous changes.

activejob/lib/active_job/arguments.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def serialize_argument(argument) # :nodoc:
5050
end
5151
end
5252
when Symbol
53-
{ OBJECT_SERIALIZER_KEY => "ActiveJob::Serializers::SymbolSerializer", "value" => argument.name }
53+
{ Serializers::OBJECT_SERIALIZER_KEY => "ActiveJob::Serializers::SymbolSerializer", "value" => argument.name }
5454
when GlobalID::Identification
5555
convert_to_global_id_hash(argument)
5656
when Array
@@ -94,15 +94,13 @@ def deserialize(arguments)
9494
RUBY2_KEYWORDS_KEY = "_aj_ruby2_keywords"
9595
# :nodoc:
9696
WITH_INDIFFERENT_ACCESS_KEY = "_aj_hash_with_indifferent_access"
97-
# :nodoc:
98-
OBJECT_SERIALIZER_KEY = "_aj_serialized"
9997

10098
# :nodoc:
10199
RESERVED_KEYS = [
102100
GLOBALID_KEY, GLOBALID_KEY.to_sym,
103101
SYMBOL_KEYS_KEY, SYMBOL_KEYS_KEY.to_sym,
104102
RUBY2_KEYWORDS_KEY, RUBY2_KEYWORDS_KEY.to_sym,
105-
OBJECT_SERIALIZER_KEY, OBJECT_SERIALIZER_KEY.to_sym,
103+
Serializers::OBJECT_SERIALIZER_KEY, Serializers::OBJECT_SERIALIZER_KEY.to_sym,
106104
WITH_INDIFFERENT_ACCESS_KEY, WITH_INDIFFERENT_ACCESS_KEY.to_sym,
107105
].to_set
108106
private_constant :RESERVED_KEYS, :GLOBALID_KEY,
@@ -136,7 +134,7 @@ def deserialize_global_id(hash)
136134
end
137135

138136
def custom_serialized?(hash)
139-
hash.key?(OBJECT_SERIALIZER_KEY)
137+
hash.key?(Serializers::OBJECT_SERIALIZER_KEY)
140138
end
141139

142140
def serialize_hash(argument)
@@ -197,4 +195,6 @@ def convert_to_global_id_hash(argument)
197195
"without an id. (Maybe you forgot to call save?)"
198196
end
199197
end
198+
199+
ActiveSupport.run_load_hooks(:active_job_arguments, Arguments)
200200
end

activejob/lib/active_job/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Railtie < Rails::Railtie # :nodoc:
2020
end
2121

2222
initializer "active_job.custom_serializers" do |app|
23-
ActiveSupport.on_load(:active_job) do
23+
ActiveSupport.on_load(:active_job_arguments) do
2424
custom_serializers = app.config.active_job.custom_serializers
2525
ActiveJob::Serializers.add_serializers custom_serializers
2626
end

activejob/lib/active_job/serializers.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def serialize(argument)
3838
# Will look up through all known serializers.
3939
# If no serializer found will raise <tt>ArgumentError</tt>.
4040
def deserialize(argument)
41-
serializer_name = argument[Arguments::OBJECT_SERIALIZER_KEY]
41+
serializer_name = argument[OBJECT_SERIALIZER_KEY]
4242
raise ArgumentError, "Serializer name is not present in the argument: #{argument.inspect}" unless serializer_name
4343

4444
serializer = serializer_name.safe_constantize
@@ -100,6 +100,9 @@ def index_serializers(new_serializers)
100100
end
101101
end
102102

103+
# :nodoc:
104+
OBJECT_SERIALIZER_KEY = "_aj_serialized"
105+
103106
add_serializers SymbolSerializer.instance,
104107
DurationSerializer.instance,
105108
DateTimeSerializer.instance,

activejob/lib/active_job/serializers/object_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class << self
3030

3131
def initialize
3232
super
33-
@template = { Arguments::OBJECT_SERIALIZER_KEY => self.class.name }.freeze
33+
@template = { Serializers::OBJECT_SERIALIZER_KEY => self.class.name }.freeze
3434
end
3535

3636
# Determines if an argument should be serialized by a serializer.

railties/test/application/active_job_railtie_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,23 @@ class FooJob < ActiveJob::Base
2020

2121
assert_equal "false", rails("runner", "p FooJob.enqueue_after_transaction_commit").strip
2222
end
23+
24+
test "custom serializers are loaded for Arguments#serialize" do
25+
app_file "config/initializers/custom_serializers.rb", <<~RUBY
26+
class Money; end
27+
28+
class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
29+
def klass = Money
30+
def serialize(money) = {}
31+
def deserialize(hash) = Money.new
32+
end
33+
34+
Rails.configuration.active_job.custom_serializers << MoneySerializer
35+
RUBY
36+
37+
app "development"
38+
39+
assert_equal([{}], ActiveJob::Arguments.serialize([Money.new]))
40+
end
2341
end
2442
end

railties/test/application/configuration_test.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3244,26 +3244,6 @@ class Post < ActiveRecord::Base
32443244
assert_not ActiveSupport.parallelize_test_databases
32453245
end
32463246

3247-
test "custom serializers should be able to set via config.active_job.custom_serializers in an initializer" do
3248-
class ::DummySerializer < ActiveJob::Serializers::ObjectSerializer
3249-
def klass
3250-
nil
3251-
end
3252-
end
3253-
3254-
app_file "config/initializers/custom_serializers.rb", <<-RUBY
3255-
Rails.application.config.active_job.custom_serializers << DummySerializer
3256-
RUBY
3257-
3258-
app "development"
3259-
3260-
assert_nothing_raised do
3261-
ActiveJob::Base
3262-
end
3263-
3264-
assert_includes ActiveJob::Serializers.serializers, DummySerializer.instance
3265-
end
3266-
32673247
test "config.active_job.verbose_enqueue_logs defaults to true in development" do
32683248
build_app
32693249
app "development"

0 commit comments

Comments
 (0)