Skip to content

Commit

Permalink
fix: make getters convert uuid to string when calling toObject() and …
Browse files Browse the repository at this point in the history
…toJSON()

Fix #14869
  • Loading branch information
vkarpov15 committed Sep 16, 2024
1 parent 1b18d5b commit 762d063
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
27 changes: 12 additions & 15 deletions lib/schema/uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ function hex2buffer(hex) {
return buff;
}

/**
* Helper function to convert the buffer input to a string
* @param {Buffer} buf The buffer to convert to a hex-string
* @returns {String} The buffer as a hex-string
* @api private
*/

function binary2hex(buf) {
// use buffer built-in function to convert from buffer to hex-string
const hex = buf != null && buf.toString('hex');
return hex;
}

/**
* Convert a String to Binary
* @param {String} uuidStr The value to process
Expand Down Expand Up @@ -67,7 +54,7 @@ function binaryToString(uuidBin) {
// i(hasezoey) dont quite know why, but "uuidBin" may sometimes also be the already processed string
let hex;
if (typeof uuidBin !== 'string' && uuidBin != null) {
hex = binary2hex(uuidBin);
hex = uuidBin != null && uuidBin.toString('hex');
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);
return uuidStr;
}
Expand All @@ -90,7 +77,17 @@ function SchemaUUID(key, options) {
if (value != null && value.$__ != null) {
return value;
}
return binaryToString(value);
if (Buffer.isBuffer(value)) {
return binaryToString(value);
} else if (value instanceof Binary) {
if (value instanceof Binary) {

Check failure on line 83 in lib/schema/uuid.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 6 spaces but found 4

Check failure on line 83 in lib/schema/uuid.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 6 spaces but found 4
return binaryToString(value.buffer);

Check failure on line 84 in lib/schema/uuid.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 8 spaces but found 6

Check failure on line 84 in lib/schema/uuid.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 8 spaces but found 6
}

Check failure on line 85 in lib/schema/uuid.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 6 spaces but found 4

Check failure on line 85 in lib/schema/uuid.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 6 spaces but found 4
} else if (utils.isPOJO(value) && value.type === 'Buffer' && Array.isArray(value.data)) {
// Cloned buffers look like `{ type: 'Buffer', data: [5, 224, ...] }`
return binaryToString(Buffer.from(value.data));
}
return value;
});
}

Expand Down
37 changes: 37 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11131,4 +11131,41 @@ describe('model: populate:', function() {
}
assert.equal(posts.length, 2);
});

it('handles converting uuid documents to strings when calling toObject() (gh-14869)', async function() {
const nodeSchema = new Schema({ _id: { type: 'UUID' }, name: 'String' });
const rootSchema = new Schema({
_id: { type: 'UUID' },
status: 'String',
node: [{ type: 'UUID', ref: 'Child' }]
});

const Node = db.model('Child', nodeSchema);
const Root = db.model('Parent', rootSchema);

const node = new Node({
_id: '65c7953e-c6e9-4c2f-8328-fe2de7df560d',
name: 'test'
});
await node.save();

const root = new Root({
_id: '05c7953e-c6e9-4c2f-8328-fe2de7df560d',
status: 'ok',
node: [node._id]
});
await root.save();

const foundRoot = await Root.findById(root._id).populate('node');

let doc = foundRoot.toJSON({ getters: true });
assert.strictEqual(doc._id, '05c7953e-c6e9-4c2f-8328-fe2de7df560d');
assert.strictEqual(doc.node.length, 1);
assert.strictEqual(doc.node[0]._id, '65c7953e-c6e9-4c2f-8328-fe2de7df560d');

doc = foundRoot.toObject({ getters: true });
assert.strictEqual(doc._id, '05c7953e-c6e9-4c2f-8328-fe2de7df560d');
assert.strictEqual(doc.node.length, 1);
assert.strictEqual(doc.node[0]._id, '65c7953e-c6e9-4c2f-8328-fe2de7df560d');
});
});

0 comments on commit 762d063

Please sign in to comment.