Skip to content

Commit 8cf6844

Browse files
jamisCopilotcomandeo-mongo
authored
RUBY-3667 BSON::ObjectId(object_id) returns its argument when it is already an ObjectId (#374)
* RUBY-3667 BSON::ObjectId(object_id) returns its argument when it is already an ObjectId * grammar fix Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * RUBY-3869 Validate Binary subtype 0x02 outer/inner length on decode (#370) * RUBY-3872 Fix put_double TypeError message regex for Ruby head (#373) --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Dmitry Rybakov <160598371+comandeo-mongo@users.noreply.github.com>
1 parent f74652c commit 8cf6844

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

lib/bson.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,25 @@
2121
# @since 0.0.0
2222
module BSON
2323

24-
# Create a new object id from a string using ObjectId.from_string
24+
# Interprets the argument as an ObjectId. If given a BSON::ObjectId, this method
25+
# returns it directly without creating a new one. Otherwise, the argument is
26+
# interpreted as a string and parsed accordingly.
2527
#
2628
# @example Create an object id from the string.
27-
# BSON::ObjectId(id)
29+
# BSON::ObjectId('...')
2830
#
29-
# @param [ String ] string The string to create the id from.
31+
# @param [ String | BSON::ObjectId ] string_or_object_id The string to create the id from,
32+
# or the existing object id to return.
3033
#
3134
# @raise [ BSON::Error::InvalidObjectId ] If the provided string is invalid.
3235
#
33-
# @return [ BSON::ObjectId ] The new object id.
36+
# @return [ BSON::ObjectId ] The new or existing object id.
3437
#
3538
# @see ObjectId.from_string
36-
def self.ObjectId(string)
37-
self::ObjectId.from_string(string)
39+
def self.ObjectId(string_or_object_id)
40+
return string_or_object_id if string_or_object_id.is_a?(self::ObjectId)
41+
42+
self::ObjectId.from_string(string_or_object_id)
3843
end
3944

4045
# Constant for binary string encoding.

spec/bson_spec.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,23 @@
1919

2020
describe ".ObjectId" do
2121

22-
let(:string) { "4e4d66343b39b68407000001" }
22+
context 'when given a string' do
23+
let(:string) { "4e4d66343b39b68407000001" }
2324

24-
it "returns an BSON::ObjectId from given string" do
25-
expect(described_class::ObjectId(string)).to be_a BSON::ObjectId
26-
expect(described_class::ObjectId(string)).to eq BSON::ObjectId.from_string(string)
25+
it "returns a BSON::ObjectId from given string" do
26+
expect(described_class::ObjectId(string)).to be_a BSON::ObjectId
27+
expect(described_class::ObjectId(string)).to eq BSON::ObjectId.from_string(string)
28+
end
29+
end
30+
31+
context 'when given an object id' do
32+
let(:object_id) do
33+
described_class::ObjectId.new
34+
end
35+
36+
it 'returns the same object' do
37+
expect(described_class::ObjectId(object_id)).to be(object_id)
38+
end
2739
end
2840
end
2941

0 commit comments

Comments
 (0)