Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Jul 28, 2022
1 parent 9d0315c commit 0977983
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/parser/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as constants from '../constants';
import { DBRef, DBRefLike, isDBRefLike } from '../db_ref';
import { Decimal128 } from '../decimal128';
import { Double } from '../double';
import { BSONError } from '../error';
import { BSONError, BSONTypeError } from '../error';
import { Int32 } from '../int_32';
import { Long } from '../long';
import { MaxKey } from '../max_key';
Expand Down Expand Up @@ -416,6 +416,8 @@ function deserializeObject(
throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
}

if (promoteUUIDs && subType !== 4) throw new BSONTypeError('Invalid type for promoteUUIDs');

if (promoteUUIDs && subType === 4) {
value = new Binary(buffer.slice(index, index + binarySize), subType).toUUID();
} else if (promoteBuffers && promoteValues) {
Expand Down
25 changes: 24 additions & 1 deletion test/node/uuid_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,34 @@ describe('UUID', () => {
});

describe('deserialize', () => {
const serializedUUID = BSON.serialize({ uuid: new BSON.UUID().toBinary() });
const originalUUID = new BSON.UUID();
const binaryUUID = originalUUID.toBinary();
const serializedUUID = BSON.serialize({ uuid: originalUUID.toBinary() });

it('should promoteUUIDs when flag is true', () => {
const { uuid: promotedUUID } = BSON.deserialize(serializedUUID, { promoteUUIDs: true });
expect(promotedUUID._bsontype).to.equal('UUID');
expect(promotedUUID).to.deep.equal(originalUUID);
});

it('should not promoteUUIDs when flag is false', () => {
const { uuid: unpromotedUUID } = BSON.deserialize(serializedUUID, { promoteUUIDs: false });
expect(unpromotedUUID._bsontype).to.equal('Binary');
expect(unpromotedUUID).to.deep.equal(binaryUUID);
});

it('should not promoteUUIDs when flag is omitted', () => {
const { uuid: omittedFlagUUID } = BSON.deserialize(serializedUUID);
expect(omittedFlagUUID._bsontype).to.equal('Binary');
expect(omittedFlagUUID).to.deep.equal(binaryUUID);
});

it('should throw BSONTypeError if _bsontype is not UUID and promoteUUIDs is true', () => {
const binaryVar = new Binary(Buffer.from('abc'));
const serializedBinary = BSON.serialize(binaryVar);
expect(() => {
BSON.deserialize(serializedBinary, { promoteUUIDs: true });
}).to.throw(BSONTypeError);
});

describe('promoteBuffers', () => {
Expand All @@ -196,6 +210,15 @@ describe('UUID', () => {
it(`should deserialize to ${outcome} type when promoteUUIDs is ${options.promoteUUIDs} and promoteBuffers is ${options.promoteBuffers}`, () => {
const { uuid } = BSON.deserialize(serializedUUID, options);
expect(uuid._bsontype).to.equal(outcome);
if (uuid._bsontype === 'UUID') {
expect(uuid.id).to.deep.equal(originalUUID.id);
} else if (uuid._bsontype === 'Binary') {
expect(uuid.buffer).to.deep.equal(originalUUID.id);
} else if (uuid._bsontype === undefined) {
expect(uuid).to.deep.equal(originalUUID.id);
} else {
expect.fail('Unexpected _bsontype');
}
});
}
});
Expand Down

0 comments on commit 0977983

Please sign in to comment.