Repository navigation
fix(mongodb-schema): only trust _bsontype on BSON values - #972
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟢 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
esvm
approved these changes
Nov 16, 2026
|
Coverage is 84.121% — COMPASS-11379 into main. No base build found for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
getBSONTypeused to take the_bsontypeproperty of any object as the field type. A document stored in the database can contain a user-defined_bsontypefield, for example{ a: { _bsontype: 'String' } }. In that case:Document;"String"or"Binary"made schema analysis throw, because the analyzer then treated the plain object as that BSON type.This adds a
getBSONTypeTaghelper that trusts_bsontypeonly on real BSON values:Symbol.for('@@mdb.bson.version')._bsontypeas 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.toStringpath and are typedDocument. BothgetBSONTypeandextractStringValueFromBSONuse the helper.Tests
Added
test/schema-analyzer.test.ts:a plain object with
_bsontypeis typedDocument, 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
_bsontypeand 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