File tree Expand file tree Collapse file tree 6 files changed +84
-0
lines changed Expand file tree Collapse file tree 6 files changed +84
-0
lines changed Original file line number Diff line number Diff line change @@ -372,6 +372,14 @@ for details on driver options.
372
372
# further information. (default: true)
373
373
use_activesupport_time_zone: true
374
374
375
+ # In Mongoid 7.3.3 and earlier, the BSON::ObjectId#as_json method would
376
+ # default to returning { "$oid" => id.to_s }. Turning on this flag will
377
+ # defer the as_json call to bson-ruby's implementation of
378
+ # BSON::ObjectId#as_json. In bson-ruby 5.x.x as_json will return
379
+ # the id as a string. In bson-ruby 4.x.x as_json will return
380
+ # { "$oid" => id.to_s }. (default: false)
381
+ use_bson_ruby_as_json: false
382
+
375
383
# Return stored times as UTC. See the time zone section below for
376
384
# further information. Most applications should not use this option.
377
385
# (default: false)
Original file line number Diff line number Diff line change @@ -118,6 +118,10 @@ Mongoid 7.4 and later will inherit the new implementation provided by
118
118
``bson-ruby`` while Mongoid 7.3 and earlier will continue with the
119
119
implementation returning a hash of ``{"$oid" => "..."}``.
120
120
121
+ .. note::
122
+
123
+ In order to get this functionality, the ``Mongoid.use_bson_ruby_as_json``
124
+ feature flag must be turned on.
121
125
122
126
Scoped Associations
123
127
-------------------
Original file line number Diff line number Diff line change @@ -103,6 +103,10 @@ module Config
103
103
# function.
104
104
option :compare_time_by_ms , default : false
105
105
106
+ # Use bson-ruby's implementation of as_json for BSON::ObjectId instead of
107
+ # the one monkey-patched into Mongoid.
108
+ option :use_bson_ruby_as_json , default : false
109
+
106
110
# Has Mongoid been configured? This is checking that at least a valid
107
111
# client config exists.
108
112
#
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
+ class BSON ::ObjectId
4
+ alias :bson_ruby_as_json :as_json
5
+
6
+ # Return a string representation of the object id for use in
7
+ # application-level JSON serialization.
8
+ #
9
+ # @example Get the object id as a JSON-serializable object.
10
+ # object_id.as_json
11
+ #
12
+ # @return [ String ] The object id as a string.
13
+ def as_json ( *args )
14
+ if Mongoid ::use_bson_ruby_as_json
15
+ bson_ruby_as_json ( *args )
16
+ else
17
+ { "$oid" => to_s }
18
+ end
19
+ end
20
+ end
21
+
3
22
class BSON ::Document
4
23
# We need to override this as ActiveSupport creates a new Object, instead of a new Hash
5
24
# see https://github.com/rails/rails/commit/f1bad130d0c9bd77c94e43b696adca56c46a66aa
Original file line number Diff line number Diff line change 321
321
it_behaves_like "a config option"
322
322
end
323
323
324
+ context 'when setting the use_bson_ruby_as_json option in the config' do
325
+ let ( :option ) { :use_bson_ruby_as_json }
326
+ let ( :default ) { false }
327
+
328
+ it_behaves_like "a config option"
329
+ end
330
+
324
331
describe "#load!" do
325
332
326
333
before ( :all ) do
Original file line number Diff line number Diff line change 2
2
3
3
require "spec_helper"
4
4
5
+ describe BSON ::ObjectId do
6
+
7
+ describe "#as_json" do
8
+
9
+ let ( :object_id ) do
10
+ described_class . new
11
+ end
12
+
13
+ context "when use_bson_ruby_as_json is set" do
14
+ around do |example |
15
+ saved_flag = Mongoid . use_bson_ruby_as_json
16
+ Mongoid . use_bson_ruby_as_json = true
17
+ begin
18
+ example . run
19
+ ensure
20
+ Mongoid . use_bson_ruby_as_json = saved_flag
21
+ end
22
+ end
23
+
24
+ it "uses bson-ruby's implementation of as_json" do
25
+ expect ( object_id . as_json ) . to eq ( object_id . bson_ruby_as_json )
26
+ end
27
+ end
28
+
29
+ context "when use_bson_ruby_as_json is not set" do
30
+ around do |example |
31
+ saved_flag = Mongoid . use_bson_ruby_as_json
32
+ Mongoid . use_bson_ruby_as_json = false
33
+ begin
34
+ example . run
35
+ ensure
36
+ Mongoid . use_bson_ruby_as_json = saved_flag
37
+ end
38
+ end
39
+
40
+ it "returns the $oid plus string" do
41
+ expect ( object_id . as_json ) . to eq ( "$oid" => object_id . to_s )
42
+ end
43
+ end
44
+ end
45
+ end
46
+
5
47
describe BSON ::Document do
6
48
7
49
describe "#symbolize_keys" do
You can’t perform that action at this time.
0 commit comments