Skip to content

Commit 1a3ef6a

Browse files
authored
MONGOID-5234 Add Feature Flag: Remove BSON::ObjectId#as_json override (#5141)
* Revert "MONGOID-???? - Remove BSON::ObjectId#as_json as we will defer to the bson-ruby gem implementation. (#5057)" This reverts commit e8aae88. * MONGOID-5234 add feature flag for as_json * MONGOID-5234 add docs for new feature flag * MONGOID-5234 remove irrelevant tests and add docstring * MONGOID-5234 add default to configuration docs * MONGOID-5234 use dots instead of :: * MONGOID-5234 put test back * MONGOID-5234 update documentation and flip the flag * MONGOID-5234 fix up as_json comment * MONGOID-5234 update release notes * MONGOID-5234 change method signature
1 parent 83d389b commit 1a3ef6a

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

docs/reference/configuration.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ for details on driver options.
372372
# further information. (default: true)
373373
use_activesupport_time_zone: true
374374

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+
375383
# Return stored times as UTC. See the time zone section below for
376384
# further information. Most applications should not use this option.
377385
# (default: false)

docs/release-notes/mongoid-7.4.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ Mongoid 7.4 and later will inherit the new implementation provided by
118118
``bson-ruby`` while Mongoid 7.3 and earlier will continue with the
119119
implementation returning a hash of ``{"$oid" => "..."}``.
120120

121+
.. note::
122+
123+
In order to get this functionality, the ``Mongoid.use_bson_ruby_as_json``
124+
feature flag must be turned on.
121125

122126
Scoped Associations
123127
-------------------

lib/mongoid/config.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ module Config
103103
# function.
104104
option :compare_time_by_ms, default: false
105105

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+
106110
# Has Mongoid been configured? This is checking that at least a valid
107111
# client config exists.
108112
#

lib/mongoid/extensions.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
# frozen_string_literal: true
22

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+
322
class BSON::Document
423
# We need to override this as ActiveSupport creates a new Object, instead of a new Hash
524
# see https://github.com/rails/rails/commit/f1bad130d0c9bd77c94e43b696adca56c46a66aa

spec/mongoid/config_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,13 @@
321321
it_behaves_like "a config option"
322322
end
323323

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+
324331
describe "#load!" do
325332

326333
before(:all) do

spec/mongoid/extensions_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,48 @@
22

33
require "spec_helper"
44

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+
547
describe BSON::Document do
648

749
describe "#symbolize_keys" do

0 commit comments

Comments
 (0)