Skip to content

Commit 1962996

Browse files
NODE-4535-automatically-promote-UUIDs
1 parent 1bbf163 commit 1962996

File tree

6 files changed

+26
-151
lines changed

6 files changed

+26
-151
lines changed

src/binary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export class Binary {
278278
if (!data) {
279279
throw new BSONTypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
280280
}
281-
return new Binary(data, type);
281+
return type === 4 ? new UUID(data) : new Binary(data, type);
282282
}
283283

284284
/** @internal */

src/parser/deserializer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ function deserializeObject(
417417
value = buffer.slice(index, index + binarySize);
418418
} else {
419419
value = new Binary(buffer.slice(index, index + binarySize), subType);
420+
if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
421+
value = value.toUUID();
422+
}
420423
}
421424
} else {
422425
const _buffer = Buffer.alloc(binarySize);

test/node/bson_compliance_test.js

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22

33
const Buffer = require('buffer').Buffer;
44
const BSON = require('../register-bson');
5-
const Code = BSON.Code;
6-
const Binary = BSON.Binary;
7-
const Timestamp = BSON.Timestamp;
8-
const Long = BSON.Long;
9-
const ObjectId = BSON.ObjectId;
10-
const DBRef = BSON.DBRef;
11-
const MinKey = BSON.MinKey;
12-
const MaxKey = BSON.MaxKey;
135

146
describe('BSON Compliance', function () {
157
/**
@@ -36,77 +28,4 @@ describe('BSON Compliance', function () {
3628

3729
done();
3830
});
39-
40-
/**
41-
* @ignore
42-
*/
43-
it('Pass all valid BSON serialization scenarios ./compliance/valid.json', function (done) {
44-
// Read and parse the json file
45-
const scenarios = require('./compliance/valid');
46-
47-
// Translate extended json to correctly typed doc
48-
function translate(doc, object) {
49-
for (let name in doc) {
50-
if (
51-
typeof doc[name] === 'number' ||
52-
typeof doc[name] === 'string' ||
53-
typeof doc[name] === 'boolean'
54-
) {
55-
object[name] = doc[name];
56-
} else if (Array.isArray(doc[name])) {
57-
object[name] = translate(doc[name], []);
58-
} else if (doc[name]['$numberLong']) {
59-
object[name] = Long.fromString(doc[name]['$numberLong']);
60-
} else if (doc[name]['$undefined']) {
61-
object[name] = null;
62-
} else if (doc[name]['$date']) {
63-
const date = new Date();
64-
date.setTime(parseInt(doc[name]['$date']['$numberLong'], 10));
65-
object[name] = date;
66-
} else if (doc[name]['$regexp']) {
67-
object[name] = new RegExp(doc[name]['$regexp'], doc[name]['$options'] || '');
68-
} else if (doc[name]['$oid']) {
69-
object[name] = new ObjectId(doc[name]['$oid']);
70-
} else if (doc[name]['$binary']) {
71-
object[name] = new Binary(doc[name]['$binary'], doc[name]['$type'] || 1);
72-
} else if (doc[name]['$timestamp']) {
73-
object[name] = Timestamp.fromBits(
74-
parseInt(doc[name]['$timestamp']['t'], 10),
75-
parseInt(doc[name]['$timestamp']['i'])
76-
);
77-
} else if (doc[name]['$ref']) {
78-
object[name] = new DBRef(doc[name]['$ref'], doc[name]['$id'], doc[name]['$db']);
79-
} else if (doc[name]['$minKey']) {
80-
object[name] = new MinKey();
81-
} else if (doc[name]['$maxKey']) {
82-
object[name] = new MaxKey();
83-
} else if (doc[name]['$code']) {
84-
object[name] = new Code(doc[name]['$code'], doc[name]['$scope'] || {});
85-
} else if (doc[name] != null && typeof doc[name] === 'object') {
86-
object[name] = translate(doc[name], {});
87-
}
88-
}
89-
90-
return object;
91-
}
92-
93-
// Iterate over all the results
94-
scenarios.documents.forEach(function (doc) {
95-
if (doc.skip) return;
96-
// Create a buffer containing the payload
97-
const expectedData = Buffer.from(doc.encoded, 'hex');
98-
// Get the expectedDocument
99-
const expectedDocument = translate(doc.document, {});
100-
// Serialize to buffer
101-
const buffer = BSON.serialize(expectedDocument);
102-
// Validate the output
103-
expect(expectedData.toString('hex')).to.equal(buffer.toString('hex'));
104-
// Attempt to deserialize
105-
const object = BSON.deserialize(buffer, { promoteLongs: false });
106-
// // Validate the object
107-
expect(JSON.stringify(expectedDocument)).to.deep.equal(JSON.stringify(object));
108-
});
109-
110-
done();
111-
});
11231
});

test/node/compliance/valid.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

test/node/extended_json_tests.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,5 +773,15 @@ Converting circular structure to EJSON:
773773
);
774774
expect(parsedUndashedInput).to.deep.equal(parsedDashedInput);
775775
});
776+
777+
it('should return UUID object when deserializing UUID subtype', () => {
778+
const exampleUUID = new BSON.UUID('878dac12-01cc-4830-b271-cbc8518e63ad');
779+
const stringifiedUUID = EJSON.stringify({ uuid: exampleUUID });
780+
const parsedUUID = EJSON.parse(stringifiedUUID);
781+
const expectedResult = {
782+
uuid: new UUID('878dac1201cc4830b271cbc8518e63ad')
783+
};
784+
expect(parsedUUID).to.deep.equal(expectedResult);
785+
});
776786
});
777787
});

test/node/uuid_tests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,16 @@ describe('UUID', () => {
191191
expect(output[18]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
192192
});
193193
});
194+
195+
describe('deserialize', () => {
196+
it('should return UUID object when deserializing UUID subtype', () => {
197+
const exampleUUID = new BSON.UUID('878dac12-01cc-4830-b271-cbc8518e63ad');
198+
const serializedUUID = BSON.serialize({ uuid: exampleUUID });
199+
const deserializedUUID = BSON.deserialize(serializedUUID);
200+
const expectedResult = {
201+
uuid: new UUID('878dac1201cc4830b271cbc8518e63ad')
202+
};
203+
expect(deserializedUUID).to.deep.equal(expectedResult);
204+
});
205+
});
194206
});

0 commit comments

Comments
 (0)