File tree Expand file tree Collapse file tree 6 files changed +109
-0
lines changed Expand file tree Collapse file tree 6 files changed +109
-0
lines changed Original file line number Diff line number Diff line change @@ -358,3 +358,6 @@ This section will be for smaller bug fixes and improvements:
358
358
a non-numeric, non-string value that implements ``:to_d`` will return a string
359
359
rather than a ``BigDecimal``
360
360
`MONGOID-5507 <https://jira.mongodb.org/browse/MONGOID-5507>`_.
361
+ - Added support for serializing and deserializing BSON::ObjectId values
362
+ when passed as ActiveJob arguments
363
+ `MONGOID-5611 <https://jira.mongodb.org/browse/MONGOID-5611>`_.
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ def standard_dependencies
36
36
end
37
37
38
38
group :test do
39
+ gem 'activejob'
39
40
gem 'timecop'
40
41
gem 'rspec-retry'
41
42
gem 'benchmark-ips'
Original file line number Diff line number Diff line change @@ -118,6 +118,17 @@ def handle_configuration_error(e)
118
118
::Mongoid ::Railties ::ControllerRuntime ::Collector . new
119
119
end
120
120
121
+ # Add custom serializers for BSON::ObjectId
122
+ initializer 'mongoid.active_job.custom_serializers' do
123
+ require 'mongoid/railties/bson_object_id_serializer'
124
+
125
+ config . after_initialize do
126
+ ActiveJob ::Serializers . add_serializers (
127
+ [ ::Mongoid ::Railties ::ActiveJobSerializers ::BsonObjectIdSerializer ]
128
+ )
129
+ end
130
+ end
131
+
121
132
end
122
133
end
123
134
end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Railties
5
+ module ActiveJobSerializers
6
+ # This class provides serialization and deserialization of BSON::ObjectId
7
+ # for ActiveJob.
8
+ #
9
+ # It is important that this class is loaded only when Rails is available
10
+ # since it depends on Rails' ActiveJob::Serializers::ObjectSerializer.
11
+ class BsonObjectIdSerializer < ActiveJob ::Serializers ::ObjectSerializer
12
+ # Returns whether the argument can be serialized by this serializer.
13
+ #
14
+ # @param [ Object ] argument The argument to check.
15
+ #
16
+ # @return [ true | false ] Whether the argument can be serialized.
17
+ def serialize? ( argument )
18
+ argument . is_a? ( BSON ::ObjectId )
19
+ end
20
+
21
+ # Serializes the argument to be passed to the job.
22
+ #
23
+ # @param [ BSON::ObjectId ] object The object to serialize.
24
+ def serialize ( object )
25
+ object . to_s
26
+ end
27
+
28
+ # Deserializes the argument back into a BSON::ObjectId.
29
+ #
30
+ # @param [ String ] string The string to deserialize.
31
+ #
32
+ # @return [ BSON::ObjectId ] The deserialized object.
33
+ def deserialize ( string )
34
+ BSON ::ObjectId . from_string ( string )
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ require 'spec_helper'
5
+ require 'active_job'
6
+ require 'mongoid/railties/bson_object_id_serializer'
7
+
8
+ describe 'ActiveJob Serialization' do
9
+ skip unless defined? ( ActiveJob )
10
+
11
+ class TestBsonObjectIdSerializerJob < ActiveJob ::Base
12
+ def perform ( *args )
13
+ args
14
+ end
15
+ end
16
+
17
+ let ( :band ) do
18
+ Band . create!
19
+ end
20
+
21
+ before do
22
+ ActiveJob ::Serializers . add_serializers (
23
+ [ ::Mongoid ::Railties ::ActiveJobSerializers ::BsonObjectIdSerializer ]
24
+ )
25
+ end
26
+
27
+ it 'serializes and deserializes BSON::ObjectId' do
28
+ expect do
29
+ TestBsonObjectIdSerializerJob . perform_later ( band . id )
30
+ end . not_to raise_error
31
+ end
32
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ require 'spec_helper'
5
+ require 'active_job'
6
+ require 'mongoid/railties/bson_object_id_serializer'
7
+
8
+ describe Mongoid ::Railties ::ActiveJobSerializers ::BsonObjectIdSerializer do
9
+ let ( :serializer ) { described_class . instance }
10
+ let ( :object_id ) { BSON ::ObjectId . new }
11
+
12
+ describe '#serialize' do
13
+ it 'serializes BSON::ObjectId' do
14
+ expect ( serializer . serialize ( object_id ) ) . to be_a ( String )
15
+ end
16
+ end
17
+
18
+ describe '#deserialize' do
19
+ it 'deserializes BSON::ObjectId' do
20
+ expect ( serializer . deserialize ( serializer . serialize ( object_id ) ) ) . to eq ( object_id )
21
+ end
22
+ end
23
+ end
You can’t perform that action at this time.
0 commit comments