Skip to content

RUBY-3667 BSON::ObjectId(object_id) returns its argument when it is already an ObjectId#374

Open
jamis wants to merge 5 commits into
mongodb:masterfrom
jamis:ruby-3667-objid-shortcut
Open

RUBY-3667 BSON::ObjectId(object_id) returns its argument when it is already an ObjectId#374
jamis wants to merge 5 commits into
mongodb:masterfrom
jamis:ruby-3667-objid-shortcut

Conversation

@jamis

@jamis jamis commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

When given an existing ObjectId, BSON::ObjectId(object_id) should simply return its argument, rather than converting it to a string and reparsing it.

Thank you to @johnnyshields for the implementation.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the BSON::ObjectId(...) convenience constructor to avoid unnecessary string conversion + re-parsing when the argument is already a BSON::ObjectId, preserving object identity and reducing overhead.

Changes:

  • Make BSON::ObjectId(object_id) return object_id directly when it is already a BSON::ObjectId.
  • Update YARD documentation for BSON::ObjectId(...) to describe the expanded accepted input type.
  • Extend the top-level BSON spec to assert identity preservation for existing BSON::ObjectId inputs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
spec/bson_spec.rb Adds coverage that BSON::ObjectId(existing_object_id) returns the same instance, and scopes the existing string case into its own context.
lib/bson.rb Implements early return for BSON::ObjectId arguments and updates method documentation accordingly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread spec/bson_spec.rb Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants