Skip to content

RUBY-3667 - If BSON::ObjectId(...) is given an existing object ID, it should return it without creating a new one #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/bson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@
# @since 0.0.0
module BSON

# Create a new object id from a string using ObjectId.from_string
# Create a new object id from a string. If given a BSON::ObjectId object instead,
# this method returns that object id without creating a new one.
#
# @example Create an object id from the string.
# BSON::ObjectId(id)
#
# @param [ String ] string The string to create the id from.
# @param [ String | BSON::ObjectId ] string The string to create the id from,
# or the existing object id to return.
#
# @raise [ BSON::Error::InvalidObjectId ] If the provided string is invalid.
#
# @return [ BSON::ObjectId ] The new object id.
# @return [ BSON::ObjectId ] The new or existing object id.
#
# @see ObjectId.from_string
def self.ObjectId(string)
Expand Down
10 changes: 7 additions & 3 deletions lib/bson/object_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,23 @@ def from_data(data)
object_id
end

# Create a new object id from a string.
# Create a new object id from a string. If given a BSON::ObjectId object instead,
# this method returns that object id without creating a new one.
#
# @example Create an object id from the string.
# BSON::ObjectId.from_string(id)
#
# @param [ String ] string The string to create the id from.
# @param [ String | BSON::ObjectId ] string The string to create the id from,
# or the existing object id to return.
#
# @raise [ BSON::Error::InvalidObjectId ] If the provided string is invalid.
#
# @return [ BSON::ObjectId ] The new object id.
# @return [ BSON::ObjectId ] The new or existing object id.
#
# @since 2.0.0
def from_string(string)
return string if string.is_a?(self)

raise Error::InvalidObjectId, "'#{string}' is an invalid ObjectId." unless legal?(string)

from_data([ string ].pack('H*'))
Expand Down
10 changes: 10 additions & 0 deletions spec/bson/object_id_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,16 @@
}.to raise_error(BSON::Error::InvalidObjectId)
end
end

context 'when given an object id' do
let(:object_id) do
described_class.new
end

it 'returns the same object' do
expect(described_class.from_string(object_id)).to be(object_id)
end
end
end

describe ".from_time" do
Expand Down
20 changes: 16 additions & 4 deletions spec/bson_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@

describe ".ObjectId" do

let(:string) { "4e4d66343b39b68407000001" }
context 'when given a string' do
let(:string) { "4e4d66343b39b68407000001" }

it "returns an BSON::ObjectId from given string" do
expect(described_class::ObjectId(string)).to be_a BSON::ObjectId
expect(described_class::ObjectId(string)).to eq BSON::ObjectId.from_string(string)
it "returns an BSON::ObjectId from given string" do
expect(described_class::ObjectId(string)).to be_a BSON::ObjectId
expect(described_class::ObjectId(string)).to eq BSON::ObjectId.from_string(string)
end
end

context 'when given an object id' do
let(:object_id) do
described_class::ObjectId.new
end

it 'returns the same object' do
expect(described_class::ObjectId(object_id)).to be(object_id)
end
end
end

Expand Down