Skip to content

Commit 0f259e7

Browse files
committed
Raise error when serializing an anonymous class.
The ModuleSerializer does not support serializing anonymous classes because when we try to deserialize the anonymous class, it wouldn't know which class to use (since class name is nil). For this reason, ModuleSerialzier now raises an error if the class name is nil. Previously, ModuleSerializer would raise an `undefined method `constantize' for nil:NilClass` error during deserialization. It's not clear why the deserialization failed from the error. In this commit, we raise an explicit error when trying to serialize an anonymous class indicating this behaviour is not supported.
1 parent f2be2b0 commit 0f259e7

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

activejob/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Raise an `SerializationError` in `Serializer::ModuleSerializer`
2+
if the module name is not present.
3+
4+
*Veerpal Brar*
5+
6+
17
## Rails 7.0.0.alpha2 (September 15, 2021) ##
28

39
* No changes.

activejob/lib/active_job/serializers/module_serializer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module ActiveJob
44
module Serializers
55
class ModuleSerializer < ObjectSerializer # :nodoc:
66
def serialize(constant)
7+
raise SerializationError, "Serializing an anonymous class is not supported" unless constant.name
78
super("value" => constant.name)
89
end
910

activejob/test/cases/argument_serialization_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ClassArgument; end
5050
end
5151
end
5252

53-
[ Object.new, Person.find("5").to_gid ].each do |arg|
53+
[ Object.new, Person.find("5").to_gid, Class.new ].each do |arg|
5454
test "does not serialize #{arg.class}" do
5555
assert_raises ActiveJob::SerializationError do
5656
ActiveJob::Arguments.serialize [ arg ]

0 commit comments

Comments
 (0)