From 6ac0e0d9be3ea0f177062c51309900c62ed7bd7c Mon Sep 17 00:00:00 2001 From: caleberi Date: Fri, 22 Aug 2025 21:38:45 +0100 Subject: [PATCH 1/2] fix: update ObjectId handling to ensure proper validation and conversion --- lib/private/machines/private/process-native-record.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/private/machines/private/process-native-record.js b/lib/private/machines/private/process-native-record.js index 75466e731..e37b95d20 100644 --- a/lib/private/machines/private/process-native-record.js +++ b/lib/private/machines/private/process-native-record.js @@ -4,7 +4,7 @@ var assert = require('assert'); var _ = require('@sailshq/lodash'); -var ObjectId = require('mongodb').ObjectID || require('mongodb').ObjectId; +var ObjectId = require('mongodb').ObjectId; var Binary = require('mongodb').Binary; @@ -43,7 +43,7 @@ module.exports = function processNativeRecord(nativeRecord, WLModel, meta) { if (useObjectIds) { var primaryKeyColumnName = WLModel.attributes[WLModel.primaryKey].columnName; var pkValue = nativeRecord[primaryKeyColumnName]; - if (_.isObject(pkValue) && pkValue instanceof ObjectId) { + if (_.isObject(pkValue) && ObjectId.isValid(pkValue.toString())) { nativeRecord[primaryKeyColumnName] = pkValue.toString(); } else { @@ -86,7 +86,7 @@ module.exports = function processNativeRecord(nativeRecord, WLModel, meta) { // Now, if relevant, convert ObjectId foreign keys to hex strings. (i.e. for singular associations) if (!meta || !meta.modelsNotUsingObjectIds || !_.contains(meta.modelsNotUsingObjectIds, attrDef.model)) { - if (_.isObject(nativeRecord[phRecordKey]) && nativeRecord[phRecordKey] instanceof ObjectId) { + if (_.isObject(nativeRecord[phRecordKey]) && ObjectId.isValid(nativeRecord[phRecordKey].toString())) { nativeRecord[phRecordKey] = nativeRecord[phRecordKey].toString(); } else { From 4e2a93b14e2811e817ffa26fe10abb9650a18278 Mon Sep 17 00:00:00 2001 From: caleberi Date: Mon, 25 Aug 2025 14:31:40 +0100 Subject: [PATCH 2/2] fix: applying mongoose solution patch --- .../machines/private/process-native-record.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/private/machines/private/process-native-record.js b/lib/private/machines/private/process-native-record.js index e37b95d20..34e2d861e 100644 --- a/lib/private/machines/private/process-native-record.js +++ b/lib/private/machines/private/process-native-record.js @@ -6,7 +6,19 @@ var assert = require('assert'); var _ = require('@sailshq/lodash'); var ObjectId = require('mongodb').ObjectId; var Binary = require('mongodb').Binary; +var objectIdHexRegexp = /^[0-9A-Fa-f]{24}$/; +function isBsonType(obj, typename) { + return ( + obj !== null && + obj._bsontype === typename + ); +} + +function isObjectIdOrHexString(v) { + return isBsonType(v, 'ObjectId') || + (typeof v === 'string' && objectIdHexRegexp.test(v) || ObjectId.isValid(v)); +} /** @@ -43,7 +55,7 @@ module.exports = function processNativeRecord(nativeRecord, WLModel, meta) { if (useObjectIds) { var primaryKeyColumnName = WLModel.attributes[WLModel.primaryKey].columnName; var pkValue = nativeRecord[primaryKeyColumnName]; - if (_.isObject(pkValue) && ObjectId.isValid(pkValue.toString())) { + if (_.isObject(pkValue) && isObjectIdOrHexString(pkValue)) { nativeRecord[primaryKeyColumnName] = pkValue.toString(); } else { @@ -86,7 +98,7 @@ module.exports = function processNativeRecord(nativeRecord, WLModel, meta) { // Now, if relevant, convert ObjectId foreign keys to hex strings. (i.e. for singular associations) if (!meta || !meta.modelsNotUsingObjectIds || !_.contains(meta.modelsNotUsingObjectIds, attrDef.model)) { - if (_.isObject(nativeRecord[phRecordKey]) && ObjectId.isValid(nativeRecord[phRecordKey].toString())) { + if (_.isObject(nativeRecord[phRecordKey]) && isObjectIdOrHexString(nativeRecord[phRecordKey])) { nativeRecord[phRecordKey] = nativeRecord[phRecordKey].toString(); } else {