Skip to content

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

Merged
jamis merged 5 commits into
mongodb:masterfrom
jamis:ruby-3667-objid-shortcut
Jun 29, 2026
Merged

RUBY-3667 BSON::ObjectId(object_id) returns its argument when it is already an ObjectId#374
jamis merged 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
Contributor

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
@jamis jamis merged commit 8cf6844 into mongodb:master Jun 29, 2026
51 checks passed
@jamis jamis deleted the ruby-3667-objid-shortcut branch June 29, 2026 14:12
@jamis jamis added feature Adds a new feature, without breaking compatibility patch For use with the 'feature' label; indicates the PR should be treated as a patch for release purposes labels Jul 1, 2026
@jamis jamis mentioned this pull request Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adds a new feature, without breaking compatibility patch For use with the 'feature' label; indicates the PR should be treated as a patch for release purposes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants