diff --git a/packages/mongodb-schema/src/schema-analyzer.ts b/packages/mongodb-schema/src/schema-analyzer.ts index 48630bc0c..0b761c837 100644 --- a/packages/mongodb-schema/src/schema-analyzer.ts +++ b/packages/mongodb-schema/src/schema-analyzer.ts @@ -225,15 +225,32 @@ const defaultSchemaParseOptions: AllSchemaParseOptions = { storedValuesLengthLimit: 10000, }; +const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version'); + +function getBSONTypeTag(value: unknown): SchemaBSONType | undefined { + if (typeof value !== 'object' || value === null) { + return undefined; + } + const tag = (value as { _bsontype?: unknown })._bsontype; + if (typeof tag !== 'string') { + return undefined; + } + const isBSONValue = + BSON_VERSION_SYMBOL in value || + !Object.getOwnPropertyDescriptor(value, '_bsontype')?.enumerable; + return isBSONValue ? (tag as SchemaBSONType) : undefined; +} + /** * Extracts a Javascript string from a BSON type to compute unique values. */ function extractStringValueFromBSON(value: any): string { - if (value?._bsontype) { - if (['Decimal128', 'Long'].includes(value._bsontype)) { + const tag = getBSONTypeTag(value); + if (tag) { + if (['Decimal128', 'Long'].includes(tag)) { return value.toString(); } - if (['Double', 'Int32'].includes(value._bsontype)) { + if (['Double', 'Int32'].includes(tag)) { return String(value.value); } } @@ -271,10 +288,10 @@ function fieldComparator( * Returns the type of value as a string. BSON type aware. Replaces `Object` * with `Document` to avoid naming conflicts with javascript Objects. */ -function getBSONType(value: any): SchemaBSONType { - const bsonType = value?._bsontype - ? value._bsontype - : Object.prototype.toString.call(value).replace(/^\[object (\w+)\]$/, '$1'); +function getBSONType(value: unknown): SchemaBSONType { + const bsonType = + getBSONTypeTag(value) ?? + Object.prototype.toString.call(value).replace(/^\[object (\w+)\]$/, '$1'); if (bsonType === 'Object') { // In the resulting schema we rename `Object` to `Document`. return 'Document'; @@ -282,7 +299,7 @@ function getBSONType(value: any): SchemaBSONType { if (bsonType === 'Code' && (value as Code).scope) { return 'CodeWScope'; } - return bsonType; + return bsonType as SchemaBSONType; } function isNullType(type: SchemaAnalysisType): type is SchemaAnalysisNullType { diff --git a/packages/mongodb-schema/test/schema-analyzer.test.ts b/packages/mongodb-schema/test/schema-analyzer.test.ts new file mode 100644 index 000000000..11aa0ba41 --- /dev/null +++ b/packages/mongodb-schema/test/schema-analyzer.test.ts @@ -0,0 +1,51 @@ +import assert from 'assert'; +import BSON from 'bson'; + +import getSchema from '../src'; + +describe('schema analyzer', function () { + describe('BSON type detection', function () { + it('treats a plain object with _bsontype as a regular document', async function () { + const schema = await getSchema([ + { _id: 1, a: { _bsontype: 'x' } }, + { _id: 2, a: { _bsontype: 'String' } }, + { _id: 3, a: { _bsontype: 'Binary', other: 1 } }, + ]); + const a = schema.fields.find((f) => f.name === 'a'); + assert.deepStrictEqual(a?.type, 'Document'); + }); + + it('still detects real BSON values', async function () { + const schema = await getSchema([ + { _id: new BSON.ObjectId(), n: BSON.Long.fromNumber(1) }, + ]); + assert.deepStrictEqual( + schema.fields.map((f) => f.type), + ['ObjectId', 'Long'], + ); + }); + + it('detects BSON values that have no bson version symbol', async function () { + const legacyLong = Object.defineProperty( + { low: 1, high: 0 }, + '_bsontype', + { + value: 'Long', + }, + ); + const schema = await getSchema([{ n: legacyLong }]); + assert.deepStrictEqual(schema.fields[0].type, 'Long'); + }); + + it('detects BSON values by the bson version symbol', async function () { + const value = { + _bsontype: 'Long', + low: 1, + high: 0, + [Symbol.for('@@mdb.bson.version')]: 7, + }; + const schema = await getSchema([{ n: value }]); + assert.deepStrictEqual(schema.fields[0].type, 'Long'); + }); + }); +});