Skip to content

Commit cb86b92

Browse files
MONGOID-5611 ActiveJob serializer for ObjectId (#5676)
1 parent 435ba5f commit cb86b92

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed

docs/release-notes/mongoid-9.0.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,6 @@ This section will be for smaller bug fixes and improvements:
358358
a non-numeric, non-string value that implements ``:to_d`` will return a string
359359
rather than a ``BigDecimal``
360360
`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>`_.

gemfiles/standard.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def standard_dependencies
3636
end
3737

3838
group :test do
39+
gem 'activejob'
3940
gem 'timecop'
4041
gem 'rspec-retry'
4142
gem 'benchmark-ips'

lib/mongoid/railtie.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ def handle_configuration_error(e)
118118
::Mongoid::Railties::ControllerRuntime::Collector.new
119119
end
120120

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+
121132
end
122133
end
123134
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

spec/integration/active_job_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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

0 commit comments

Comments
 (0)