Skip to content

feat(NODE-4793): add Symbol.toStringTag to all BSON types#880

Closed
sarthaksoni25 wants to merge 1 commit intomongodb:mainfrom
sarthaksoni25:main
Closed

feat(NODE-4793): add Symbol.toStringTag to all BSON types#880
sarthaksoni25 wants to merge 1 commit intomongodb:mainfrom
sarthaksoni25:main

Conversation

@sarthaksoni25
Copy link
Copy Markdown

Description

Summary of Changes

Adds Symbol.toStringTag to all BSON types by implementing the getter in the BSONValue base class, delegating to this._bsontype. Since _bsontype is abstract on BSONValue and implemented by every subclass, all 13 BSON types inherit the correct tag automatically.

Before:
Object.prototype.toString.call(new ObjectId()) // '[object Object]'

After:
Object.prototype.toString.call(new ObjectId()) // '[object ObjectId]'

Notes for Reviewers

Follows the same pattern as the existing bsonType symbol getter added in NODE-7255. The implementation is a single getter on BSONValue — no changes needed in individual subclasses.

Double check the following

  • Lint is passing (npm run check:lint)
  • Self-review completed
  • PR title follows the correct format: feat(NODE-4793): add Symbol.toStringTag to all BSON types
  • Changes are covered by tests (14 new tests added in bson_type_classes.test.ts)
  • No new TODOs

@sarthaksoni25 sarthaksoni25 requested a review from a team as a code owner April 13, 2026 17:06
@sarthaksoni25 sarthaksoni25 force-pushed the main branch 2 times, most recently from 72ffb34 to c66fe9c Compare April 13, 2026 17:28
@dariakp dariakp added tracked-in-jira There is a ticket in Mongo's Jira instance tracking this issue/PR External Submission labels Apr 15, 2026
Comment on lines +105 to +109
it(`UUID inherits Symbol.toStringTag from Binary`, () =>
expect(Object.getPrototypeOf(TypeClass.prototype)).to.have.property(
Symbol.toStringTag,
'Binary'
));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approach is right - matches the existing bsonType getter pattern on BSONValue. One suggestion: override Symbol.toStringTag on UUID to return 'UUID'. _bsontype is a wire-protocol key and must stay 'Binary' (serializer dispatches on it), but Symbol.toStringTag is a developer-facing identity hint with no wire impact - making it reflect the JS class gives nicer debug output ([object UUID]) without breaking anything. The test would then assert toStringTag === TypeClass.name uniformly across all types.

Copy link
Copy Markdown
Contributor

@nbbeeken nbbeeken Apr 21, 2026

Choose a reason for hiding this comment

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

> Object.prototype.toString.call(Buffer.alloc(0))
'[object Uint8Array]'

It is not always the case that the string tag matches the class name, because UUID is a trivial subclass of Binary and Binary is the relevant BSON type it may make sense to keep these matching the _bsontype field. It also is not only a developer hint here's an example where it's a load bearing type check.

An additional benefit to adding this field is it adds the same functionality we added when we introduced the @@mdb.bson.type symbol but without the need to import/create something from the library.

Just my opinion here :) not strongly held

Copy link
Copy Markdown
Member

@tadjik1 tadjik1 left a comment

Choose a reason for hiding this comment

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

hi @sarthaksoni25, thanks for you work, everything looks correct, simple and clean implementation, well done!
I left one suggestion, please let me know what you think.

@tadjik1 tadjik1 self-assigned this Apr 21, 2026
@tadjik1 tadjik1 added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Apr 21, 2026
@addaleax
Copy link
Copy Markdown
Contributor

Should this be considered semver-major?

@tadjik1
Copy link
Copy Markdown
Member

tadjik1 commented Apr 22, 2026

hi @sarthaksoni25, after discussing this change with the team we have decided against it. the main reason is that it can break existing functionality of external packages that might rely on the string representation of BSON instances. and potential benefits do not justify complexity of the migration.
Once again, thanks a lot for your contribution!

@tadjik1 tadjik1 closed this Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Submission Primary Review In Review with primary reviewer, not yet ready for team's eyes tracked-in-jira There is a ticket in Mongo's Jira instance tracking this issue/PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants