Skip to content

Commit

Permalink
Raise error when serializing an anonymous class.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
VeerpalBrar committed Oct 13, 2021
1 parent f2be2b0 commit 0f259e7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
6 changes: 6 additions & 0 deletions activejob/CHANGELOG.md
@@ -1,3 +1,9 @@
* Raise an `SerializationError` in `Serializer::ModuleSerializer`
if the module name is not present.

*Veerpal Brar*


## Rails 7.0.0.alpha2 (September 15, 2021) ##

* No changes.
Expand Down
1 change: 1 addition & 0 deletions activejob/lib/active_job/serializers/module_serializer.rb
Expand Up @@ -4,6 +4,7 @@ module ActiveJob
module Serializers
class ModuleSerializer < ObjectSerializer # :nodoc:
def serialize(constant)
raise SerializationError, "Serializing an anonymous class is not supported" unless constant.name
super("value" => constant.name)
end

Expand Down
2 changes: 1 addition & 1 deletion activejob/test/cases/argument_serialization_test.rb
Expand Up @@ -50,7 +50,7 @@ class ClassArgument; end
end
end

[ Object.new, Person.find("5").to_gid ].each do |arg|
[ Object.new, Person.find("5").to_gid, Class.new ].each do |arg|
test "does not serialize #{arg.class}" do
assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize [ arg ]
Expand Down

0 comments on commit 0f259e7

Please sign in to comment.