Skip to content

Commit b990257

Browse files
serialize UUID class as a type
1 parent 4d13309 commit b990257

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/parser/serializer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,8 @@ export function serializeInto(
837837
);
838838
} else if (value['_bsontype'] === 'Binary') {
839839
index = serializeBinary(buffer, key, value, index, true);
840+
} else if (value['_bsontype'] === 'UUID') {
841+
index = serializeBinary(buffer, key, value.toBinary(), index);
840842
} else if (value['_bsontype'] === 'Symbol') {
841843
index = serializeSymbol(buffer, key, value, index, true);
842844
} else if (value['_bsontype'] === 'DBRef') {
@@ -938,6 +940,8 @@ export function serializeInto(
938940
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
939941
} else if (value['_bsontype'] === 'Binary') {
940942
index = serializeBinary(buffer, key, value, index);
943+
} else if (value['_bsontype'] === 'UUID') {
944+
index = serializeBinary(buffer, key, value.toBinary(), index);
941945
} else if (value['_bsontype'] === 'Symbol') {
942946
index = serializeSymbol(buffer, key, value, index);
943947
} else if (value['_bsontype'] === 'DBRef') {
@@ -1043,6 +1047,8 @@ export function serializeInto(
10431047
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
10441048
} else if (value['_bsontype'] === 'Binary') {
10451049
index = serializeBinary(buffer, key, value, index);
1050+
} else if (value['_bsontype'] === 'UUID') {
1051+
index = serializeBinary(buffer, key, value.toBinary(), index);
10461052
} else if (value['_bsontype'] === 'Symbol') {
10471053
index = serializeSymbol(buffer, key, value, index);
10481054
} else if (value['_bsontype'] === 'DBRef') {

test/node/uuid_tests.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const { inspect } = require('util');
66
const { validate: uuidStringValidate, version: uuidStringVersion } = require('uuid');
77
const BSON = require('../register-bson');
88
const BSONTypeError = BSON.BSONTypeError;
9+
const BSON_DATA_BINARY = BSON.BSON_DATA_BINARY;
10+
const BSON_BINARY_SUBTYPE_UUID_NEW = BSON.BSON_BINARY_SUBTYPE_UUID_NEW;
911

1012
// Test values
1113
const UPPERCASE_DASH_SEPARATED_UUID_STRING = 'AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA';
@@ -162,4 +164,22 @@ describe('UUID', () => {
162164
const uuid = new UUID(UPPERCASE_DASH_SEPARATED_UUID_STRING);
163165
expect(inspect(uuid)).to.equal(`new UUID("${LOWERCASE_DASH_SEPARATED_UUID_STRING}")`);
164166
});
167+
168+
it(`should serialize as a valid UUID _bsontype with Object input without error`, () => {
169+
const output = BSON.serialize({ uuid: new BSON.UUID() });
170+
expect(output[4]).to.equal(BSON_DATA_BINARY);
171+
expect(output[14]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
172+
});
173+
174+
it(`should serialize as a valid UUID _bsontype with Map input without error`, () => {
175+
const output = BSON.serialize(new Map([['uuid', new BSON.UUID()]]));
176+
expect(output[4]).to.equal(BSON_DATA_BINARY);
177+
expect(output[14]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
178+
});
179+
180+
it(`should serialize as a valid UUID _bsontype with Array input without error`, () => {
181+
const output = BSON.serialize({ a: [new BSON.UUID()] });
182+
expect(output[11]).to.equal(BSON_DATA_BINARY);
183+
expect(output[18]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
184+
});
165185
});

0 commit comments

Comments
 (0)