Skip to content

Commit

Permalink
NODE-4535-automatically-promote-UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Aug 11, 2022
1 parent 1bbf163 commit 1962996
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 151 deletions.
2 changes: 1 addition & 1 deletion src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class Binary {
if (!data) {
throw new BSONTypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
}
return new Binary(data, type);
return type === 4 ? new UUID(data) : new Binary(data, type);
}

/** @internal */
Expand Down
3 changes: 3 additions & 0 deletions src/parser/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ function deserializeObject(
value = buffer.slice(index, index + binarySize);
} else {
value = new Binary(buffer.slice(index, index + binarySize), subType);
if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
value = value.toUUID();
}
}
} else {
const _buffer = Buffer.alloc(binarySize);
Expand Down
81 changes: 0 additions & 81 deletions test/node/bson_compliance_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

const Buffer = require('buffer').Buffer;
const BSON = require('../register-bson');
const Code = BSON.Code;
const Binary = BSON.Binary;
const Timestamp = BSON.Timestamp;
const Long = BSON.Long;
const ObjectId = BSON.ObjectId;
const DBRef = BSON.DBRef;
const MinKey = BSON.MinKey;
const MaxKey = BSON.MaxKey;

describe('BSON Compliance', function () {
/**
Expand All @@ -36,77 +28,4 @@ describe('BSON Compliance', function () {

done();
});

/**
* @ignore
*/
it('Pass all valid BSON serialization scenarios ./compliance/valid.json', function (done) {
// Read and parse the json file
const scenarios = require('./compliance/valid');

// Translate extended json to correctly typed doc
function translate(doc, object) {
for (let name in doc) {
if (
typeof doc[name] === 'number' ||
typeof doc[name] === 'string' ||
typeof doc[name] === 'boolean'
) {
object[name] = doc[name];
} else if (Array.isArray(doc[name])) {
object[name] = translate(doc[name], []);
} else if (doc[name]['$numberLong']) {
object[name] = Long.fromString(doc[name]['$numberLong']);
} else if (doc[name]['$undefined']) {
object[name] = null;
} else if (doc[name]['$date']) {
const date = new Date();
date.setTime(parseInt(doc[name]['$date']['$numberLong'], 10));
object[name] = date;
} else if (doc[name]['$regexp']) {
object[name] = new RegExp(doc[name]['$regexp'], doc[name]['$options'] || '');
} else if (doc[name]['$oid']) {
object[name] = new ObjectId(doc[name]['$oid']);
} else if (doc[name]['$binary']) {
object[name] = new Binary(doc[name]['$binary'], doc[name]['$type'] || 1);
} else if (doc[name]['$timestamp']) {
object[name] = Timestamp.fromBits(
parseInt(doc[name]['$timestamp']['t'], 10),
parseInt(doc[name]['$timestamp']['i'])
);
} else if (doc[name]['$ref']) {
object[name] = new DBRef(doc[name]['$ref'], doc[name]['$id'], doc[name]['$db']);
} else if (doc[name]['$minKey']) {
object[name] = new MinKey();
} else if (doc[name]['$maxKey']) {
object[name] = new MaxKey();
} else if (doc[name]['$code']) {
object[name] = new Code(doc[name]['$code'], doc[name]['$scope'] || {});
} else if (doc[name] != null && typeof doc[name] === 'object') {
object[name] = translate(doc[name], {});
}
}

return object;
}

// Iterate over all the results
scenarios.documents.forEach(function (doc) {
if (doc.skip) return;
// Create a buffer containing the payload
const expectedData = Buffer.from(doc.encoded, 'hex');
// Get the expectedDocument
const expectedDocument = translate(doc.document, {});
// Serialize to buffer
const buffer = BSON.serialize(expectedDocument);
// Validate the output
expect(expectedData.toString('hex')).to.equal(buffer.toString('hex'));
// Attempt to deserialize
const object = BSON.deserialize(buffer, { promoteLongs: false });
// // Validate the object
expect(JSON.stringify(expectedDocument)).to.deep.equal(JSON.stringify(object));
});

done();
});
});
69 changes: 0 additions & 69 deletions test/node/compliance/valid.js

This file was deleted.

10 changes: 10 additions & 0 deletions test/node/extended_json_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,5 +773,15 @@ Converting circular structure to EJSON:
);
expect(parsedUndashedInput).to.deep.equal(parsedDashedInput);
});

it('should return UUID object when deserializing UUID subtype', () => {
const exampleUUID = new BSON.UUID('878dac12-01cc-4830-b271-cbc8518e63ad');
const stringifiedUUID = EJSON.stringify({ uuid: exampleUUID });
const parsedUUID = EJSON.parse(stringifiedUUID);
const expectedResult = {
uuid: new UUID('878dac1201cc4830b271cbc8518e63ad')
};
expect(parsedUUID).to.deep.equal(expectedResult);
});
});
});
12 changes: 12 additions & 0 deletions test/node/uuid_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,16 @@ describe('UUID', () => {
expect(output[18]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
});
});

describe('deserialize', () => {
it('should return UUID object when deserializing UUID subtype', () => {
const exampleUUID = new BSON.UUID('878dac12-01cc-4830-b271-cbc8518e63ad');
const serializedUUID = BSON.serialize({ uuid: exampleUUID });
const deserializedUUID = BSON.deserialize(serializedUUID);
const expectedResult = {
uuid: new UUID('878dac1201cc4830b271cbc8518e63ad')
};
expect(deserializedUUID).to.deep.equal(expectedResult);
});
});
});

0 comments on commit 1962996

Please sign in to comment.