Skip to content

Commit

Permalink
feat(NODE-4405): support serializing UUID class (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Jul 27, 2022
1 parent 4d75481 commit f5dc9ed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/parser/serializer.ts
Expand Up @@ -837,6 +837,8 @@ export function serializeInto(
);
} else if (value['_bsontype'] === 'Binary') {
index = serializeBinary(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'UUID') {
index = serializeBinary(buffer, key, value.toBinary(), index);
} else if (value['_bsontype'] === 'Symbol') {
index = serializeSymbol(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'DBRef') {
Expand Down Expand Up @@ -938,6 +940,8 @@ export function serializeInto(
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
} else if (value['_bsontype'] === 'Binary') {
index = serializeBinary(buffer, key, value, index);
} else if (value['_bsontype'] === 'UUID') {
index = serializeBinary(buffer, key, value.toBinary(), index);
} else if (value['_bsontype'] === 'Symbol') {
index = serializeSymbol(buffer, key, value, index);
} else if (value['_bsontype'] === 'DBRef') {
Expand Down Expand Up @@ -1043,6 +1047,8 @@ export function serializeInto(
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
} else if (value['_bsontype'] === 'Binary') {
index = serializeBinary(buffer, key, value, index);
} else if (value['_bsontype'] === 'UUID') {
index = serializeBinary(buffer, key, value.toBinary(), index);
} else if (value['_bsontype'] === 'Symbol') {
index = serializeSymbol(buffer, key, value, index);
} else if (value['_bsontype'] === 'DBRef') {
Expand Down
29 changes: 29 additions & 0 deletions test/node/uuid_tests.js
Expand Up @@ -6,6 +6,8 @@ const { inspect } = require('util');
const { validate: uuidStringValidate, version: uuidStringVersion } = require('uuid');
const BSON = require('../register-bson');
const BSONTypeError = BSON.BSONTypeError;
const BSON_DATA_BINARY = BSON.BSON_DATA_BINARY;
const BSON_BINARY_SUBTYPE_UUID_NEW = BSON.BSON_BINARY_SUBTYPE_UUID_NEW;

// Test values
const UPPERCASE_DASH_SEPARATED_UUID_STRING = 'AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA';
Expand Down Expand Up @@ -162,4 +164,31 @@ describe('UUID', () => {
const uuid = new UUID(UPPERCASE_DASH_SEPARATED_UUID_STRING);
expect(inspect(uuid)).to.equal(`new UUID("${LOWERCASE_DASH_SEPARATED_UUID_STRING}")`);
});

describe('serialize', () => {
it('should have a valid UUID _bsontype with Object input without error', () => {
const output = BSON.serialize({ uuid: new BSON.UUID() });
expect(output[4]).to.equal(BSON_DATA_BINARY);
expect(output[14]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
});

it('should have a valid UUID _bsontype with Map input without error', () => {
const output = BSON.serialize(new Map([['uuid', new BSON.UUID()]]));
expect(output[4]).to.equal(BSON_DATA_BINARY);
expect(output[14]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
});

it('should have as a valid UUID _bsontype with Array input without error', () => {
const output = BSON.serialize({ a: [new BSON.UUID()] });
expect(output[11]).to.equal(BSON_DATA_BINARY);
expect(output[18]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
});

it('should serialize BSON.UUID() input the same as BSON.UUID().toBinary()', () => {
const exampleUUID = new BSON.UUID();
const toBinarySerialization = BSON.serialize({ uuid: exampleUUID.toBinary() });
const plainUUIDSerialization = BSON.serialize({ uuid: exampleUUID });
expect(plainUUIDSerialization).to.deep.equal(toBinarySerialization);
});
});
});

0 comments on commit f5dc9ed

Please sign in to comment.