diff --git a/src/extended_json.ts b/src/extended_json.ts index 90269623b..494470304 100644 --- a/src/extended_json.ts +++ b/src/extended_json.ts @@ -1,5 +1,6 @@ import { Binary } from './binary'; import type { Document } from './bson'; +import { UUID } from './uuid'; import { Code } from './code'; import { DBRef, isDBRefLike } from './db_ref'; import { Decimal128 } from './decimal128'; @@ -32,7 +33,8 @@ type BSONType = | ObjectId | BSONRegExp | BSONSymbol - | Timestamp; + | Timestamp + | UUID; export function isBSONType(value: unknown): value is BSONType { return ( @@ -247,6 +249,7 @@ function serializeValue(value: any, options: EJSONSerializeOptions): any { const BSON_TYPE_MAPPINGS = { Binary: (o: Binary) => new Binary(o.value(), o.sub_type), + UUID: (o: UUID) => new UUID(o.id), Code: (o: Code) => new Code(o.code, o.scope), DBRef: (o: DBRef) => new DBRef(o.collection || o.namespace, o.oid, o.db, o.fields), // "namespace" for 1.x library backwards compat Decimal128: (o: Decimal128) => new Decimal128(o.bytes), diff --git a/src/uuid.ts b/src/uuid.ts index 625e57dea..0bb496aa4 100644 --- a/src/uuid.ts +++ b/src/uuid.ts @@ -4,6 +4,7 @@ import { Binary } from './binary'; import { bufferToUuidHexString, uuidHexStringToBuffer, uuidValidateString } from './uuid_utils'; import { isUint8Array, randomBytes } from './parser/utils'; import { BSONTypeError } from './error'; +import type { EJSONOptions } from './extended_json'; /** @public */ export type UUIDExtended = { @@ -204,6 +205,10 @@ export class UUID { inspect(): string { return `new UUID("${this.toHexString()}")`; } + + toExtendedJSON(options: EJSONOptions) { + return this.toBinary().toExtendedJSON(options); + } } Object.defineProperty(UUID.prototype, '_bsontype', { value: 'UUID' }); diff --git a/test/node/extended_json_tests.js b/test/node/extended_json_tests.js index e8f97457a..450d45dd0 100644 --- a/test/node/extended_json_tests.js +++ b/test/node/extended_json_tests.js @@ -19,6 +19,7 @@ const ObjectId = BSON.ObjectId; const BSONRegExp = BSON.BSONRegExp; const BSONSymbol = BSON.BSONSymbol; const Timestamp = BSON.Timestamp; +const UUID = BSON.UUID; // Several tests in this file can test interop between current library versions and library version 1.1.0, because // between 1.1.0 and 4.0.0 there was a significant rewrite. To minimize maintenance issues of a hard dependency on @@ -739,4 +740,18 @@ Converting circular structure to EJSON: }); }); }); + + describe('UUID stringify', () => { + const uuid = new UUID(); + const stringifiedPlainUUID = EJSON.stringify({ u: uuid }); + it('should return same values for UUID.toBinary() and UUID', () => { + const stringifiedToBinary = EJSON.stringify({ u: uuid.toBinary() }); + expect(stringifiedToBinary).to.deep.equal(stringifiedPlainUUID); + }); + it('should serialize to correct subType', () => { + const stringifiedUUIDtoObject = JSON.parse(stringifiedPlainUUID); + const stringifiedBinaryNewUUIDSubType = '04'; + expect(stringifiedUUIDtoObject.u.$binary.subType).to.equal(stringifiedBinaryNewUUIDSubType); + }); + }); });