From e57e462a628f49c0a46dbed81a2a6155579f6e45 Mon Sep 17 00:00:00 2001 From: Katherine Oelsner <49968061+octokatherine@users.noreply.github.com> Date: Mon, 20 Nov 2023 23:26:04 +0000 Subject: [PATCH] ActiveJob: fallback serializing String subclasses https://github.com/rails/rails/pull/50090 broke serialization of String subclasses that don't have serializers, like ActiveSupport::SafeBuffer. Co-authored-by: John Hawthorn --- activejob/lib/active_job/arguments.rb | 6 +++++- activejob/test/cases/argument_serialization_test.rb | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/activejob/lib/active_job/arguments.rb b/activejob/lib/active_job/arguments.rb index 5de4bc709843d..4f144c5635d4c 100644 --- a/activejob/lib/active_job/arguments.rb +++ b/activejob/lib/active_job/arguments.rb @@ -76,7 +76,11 @@ def serialize_argument(argument) if argument.class == String argument else - Serializers.serialize(argument) + begin + Serializers.serialize(argument) + rescue SerializationError + argument + end end when GlobalID::Identification convert_to_global_id_hash(argument) diff --git a/activejob/test/cases/argument_serialization_test.rb b/activejob/test/cases/argument_serialization_test.rb index 4f123de8ea9ca..fec75604313ff 100644 --- a/activejob/test/cases/argument_serialization_test.rb +++ b/activejob/test/cases/argument_serialization_test.rb @@ -42,6 +42,9 @@ def klass end end + class StringWithoutSerializer < String + end + setup do @person = Person.find("5") end @@ -156,6 +159,14 @@ def klass ActiveJob::Serializers._additional_serializers = original_serializers end + test "serialize a String subclass object without a serializer" do + string_without_serializer = StringWithoutSerializer.new("foo") + serialized = ActiveJob::Arguments.serialize([string_without_serializer]) + deserialized = ActiveJob::Arguments.deserialize(JSON.load(JSON.dump(serialized))).first + assert_instance_of String, deserialized + assert_equal string_without_serializer, deserialized + end + test "serialize a hash" do symbol_key = { a: 1 } string_key = { "a" => 1 }