Skip to content

fix(mongodb-schema): only trust _bsontype on BSON values - #972

Merged
dudaschar merged 1 commit into
mainfrom
COMPASS-11379
Nov 16, 2026
Merged

dudaschar merged 1 commit into
mainfrom
COMPASS-11379

Conversation

@dudaschar

Copy link
Copy Markdown
Collaborator

Description

getBSONType used to take the _bsontype property of any object as the field type. A document stored in the database can contain a user-defined _bsontype field, for example { a: { _bsontype: 'String' } }. In that case:

  • the field was reported with whatever string was stored as its type, instead of Document;
  • values such as "String" or "Binary" made schema analysis throw, because the analyzer then treated the plain object as that BSON type.

This adds a getBSONTypeTag helper that trusts _bsontype only on real BSON values:

  • bson 5+ values are recognized by Symbol.for('@@mdb.bson.version').
  • bson 4 values have no such symbol, so they are recognized by not having _bsontype as an own enumerable property. On bson 4 it is defined on the prototype, or is non-enumerable (Timestamp).

Plain objects fall through to the existing Object.prototype.toString path and are typed Document. Both getBSONType and extractStringValueFromBSON use the helper.

Tests

Added test/schema-analyzer.test.ts:

  • a plain object with _bsontype is typed Document, including values that previously threw;

  • real bson 7 values (ObjectId, Long) are still detected;

  • a value with the bson version symbol is detected (the only case that exercises the symbol branch);

  • a bson 4-style value with a non-enumerable _bsontype and no symbol is detected.

  • I have signed the Contributor License Agreement (https://www.mongodb.com/legal/contributor-agreement)

  • New tests and/or benchmarks are included

  • Documentation is changed or added

@dudaschar dudaschar self-assigned this Nov 16, 2026
@dudaschar
dudaschar requested review from a team as code owners November 16, 2026 11:35
@dudaschar
dudaschar requested review from esvm and a balanced review from Copilot November 16, 2026 11:35

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.

🟢 Approval recommended

No active review issues are recorded for the BSON type-detection changes.

No open issues 🎉

What changed in this PR

This PR fixes BSON type detection in mongodb-schema so user-defined _bsontype fields on plain documents no longer cause incorrect types or schema-analysis errors.

Changes:

  • Adds a shared helper that recognizes BSON type tags using the version symbol or legacy property characteristics.
  • Uses the helper for type detection and string-value extraction.
  • Adds regression tests for plain documents, real BSON values, and modern and legacy detection paths.
File Description
packages/​mongodb-schema/​test/​schema-analyzer.test.ts Adds regression and BSON compatibility tests.
packages/​mongodb-schema/​src/​schema-analyzer.ts Centralizes BSON tag checks to avoid trusting ordinary document fields.

Review effort: Balanced

@dudaschar
dudaschar enabled auto-merge (squash) November 16, 2026 13:32
@dudaschar
dudaschar merged commit 37feb61 into main Nov 16, 2026
32 of 34 checks passed
@dudaschar
dudaschar deleted the COMPASS-11379 branch November 16, 2026 13:41
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage is 84.121% — COMPASS-11379 into main. No base build found for main.

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.

4 participants