Skip to content

Commit 3902283

Browse files
authored
Merge pull request Automattic#14890 from Automattic/vkarpov15/Automatticgh-14869
fix: make getters convert uuid to string when calling toObject() and toJSON()
2 parents 90f8af3 + 02162ff commit 3902283

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

lib/schema/uuid.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@ function hex2buffer(hex) {
2626
return buff;
2727
}
2828

29-
/**
30-
* Helper function to convert the buffer input to a string
31-
* @param {Buffer} buf The buffer to convert to a hex-string
32-
* @returns {String} The buffer as a hex-string
33-
* @api private
34-
*/
35-
36-
function binary2hex(buf) {
37-
// use buffer built-in function to convert from buffer to hex-string
38-
const hex = buf != null && buf.toString('hex');
39-
return hex;
40-
}
41-
4229
/**
4330
* Convert a String to Binary
4431
* @param {String} uuidStr The value to process
@@ -67,7 +54,7 @@ function binaryToString(uuidBin) {
6754
// i(hasezoey) dont quite know why, but "uuidBin" may sometimes also be the already processed string
6855
let hex;
6956
if (typeof uuidBin !== 'string' && uuidBin != null) {
70-
hex = binary2hex(uuidBin);
57+
hex = uuidBin.toString('hex');
7158
const uuidStr = hex.substring(0, 8) + '-' + hex.substring(8, 8 + 4) + '-' + hex.substring(12, 12 + 4) + '-' + hex.substring(16, 16 + 4) + '-' + hex.substring(20, 20 + 12);
7259
return uuidStr;
7360
}
@@ -90,7 +77,15 @@ function SchemaUUID(key, options) {
9077
if (value != null && value.$__ != null) {
9178
return value;
9279
}
93-
return binaryToString(value);
80+
if (Buffer.isBuffer(value)) {
81+
return binaryToString(value);
82+
} else if (value instanceof Binary) {
83+
return binaryToString(value.buffer);
84+
} else if (utils.isPOJO(value) && value.type === 'Buffer' && Array.isArray(value.data)) {
85+
// Cloned buffers look like `{ type: 'Buffer', data: [5, 224, ...] }`
86+
return binaryToString(Buffer.from(value.data));
87+
}
88+
return value;
9489
});
9590
}
9691

test/model.populate.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11131,4 +11131,41 @@ describe('model: populate:', function() {
1113111131
}
1113211132
assert.equal(posts.length, 2);
1113311133
});
11134+
11135+
it('handles converting uuid documents to strings when calling toObject() (gh-14869)', async function() {
11136+
const nodeSchema = new Schema({ _id: { type: 'UUID' }, name: 'String' });
11137+
const rootSchema = new Schema({
11138+
_id: { type: 'UUID' },
11139+
status: 'String',
11140+
node: [{ type: 'UUID', ref: 'Child' }]
11141+
});
11142+
11143+
const Node = db.model('Child', nodeSchema);
11144+
const Root = db.model('Parent', rootSchema);
11145+
11146+
const node = new Node({
11147+
_id: '65c7953e-c6e9-4c2f-8328-fe2de7df560d',
11148+
name: 'test'
11149+
});
11150+
await node.save();
11151+
11152+
const root = new Root({
11153+
_id: '05c7953e-c6e9-4c2f-8328-fe2de7df560d',
11154+
status: 'ok',
11155+
node: [node._id]
11156+
});
11157+
await root.save();
11158+
11159+
const foundRoot = await Root.findById(root._id).populate('node');
11160+
11161+
let doc = foundRoot.toJSON({ getters: true });
11162+
assert.strictEqual(doc._id, '05c7953e-c6e9-4c2f-8328-fe2de7df560d');
11163+
assert.strictEqual(doc.node.length, 1);
11164+
assert.strictEqual(doc.node[0]._id, '65c7953e-c6e9-4c2f-8328-fe2de7df560d');
11165+
11166+
doc = foundRoot.toObject({ getters: true });
11167+
assert.strictEqual(doc._id, '05c7953e-c6e9-4c2f-8328-fe2de7df560d');
11168+
assert.strictEqual(doc.node.length, 1);
11169+
assert.strictEqual(doc.node[0]._id, '65c7953e-c6e9-4c2f-8328-fe2de7df560d');
11170+
});
1113411171
});

0 commit comments

Comments
 (0)