-
Right now I am migrating from Mongoose 5.x to 6.x and got about half of integration tests fail just because of undocumented changes in ObjectId validation.
I get the expectation that once a variable has passed any of these validations, it can be safely specified both as Example: const mongoose = require('mongoose');
const testSubjects = {
'null': null,
'undefined': undefined,
'new ObjectId()': new mongoose.Types.ObjectId(),
'ObjectId().valueOf()': (new mongoose.Types.ObjectId()).valueOf(),
'ObjectId().toString()': (new mongoose.Types.ObjectId()).toString(),
'ObjectId().toHexString()': (new mongoose.Types.ObjectId()).toHexString(),
'\'Z\'.repeat(12)': 'Z'.repeat(12),
'\'F\'.repeat(24)': 'F'.repeat(24),
'\'580e0797bb495f0a200e91ad\'': '580e0797bb495f0a200e91ad',
'{ _id: new ObjectId() }': { _id: new mongoose.Types.ObjectId() },
'{ _id: \'Z\'.repeat(12) }': { _id: 'Z'.repeat(12) },
'{ _id: \'F\'.repeat(24) }': { _id: 'F'.repeat(24) },
'{ _id: \'580e0797bb495f0a200e91ad\' }': { _id: '580e0797bb495f0a200e91ad' },
'{ id: new ObjectId() }': { id: new mongoose.Types.ObjectId() },
'{ id: \'Z\'.repeat(12) }': { id: 'Z'.repeat(12) },
'{ id: \'F\'.repeat(24) }': { id: 'F'.repeat(24) },
'{ id: \'580e0797bb495f0a200e91ad\' }': { id: '580e0797bb495f0a200e91ad' },
'{ id: null }': { id: null },
'{ id: undefined }': { id: undefined },
'{ _id: null }': { _id: null },
'{ _id: undefined }': { _id: undefined },
};
console.table(Object.entries(testSubjects).map(([key, value]) => ({
test: key,
'isValidObjectId': mongoose.isValidObjectId(value),
'ObjectId.isValid': mongoose.Types.ObjectId.isValid(value),
'new ObjectId': newObjectId(value),
})));
function newObjectId(value) {
try {
return new mongoose.Types.ObjectId(value);
} catch (err) {
return 'ERROR';
}
} mongoose@6.1.6 (same for @6.1.5) + bson@4.6.1:
mongoose@6.1.5 + bson@4.6.0Note: bson@4.6.0 is demonstrated because since bson@4.6.1
mongoose@5.12.3 + bson@1.1.4And some old versions (precisely from the codebase I am migrating now)
You can see how much has changed from those "old times". Notable changes between versions
I can't help quoting a famous anecdote (translated):
MongoDB and mongoose seem to be mature products. And having a long history of changes/bugs with such base entity as ObjectId (regarding what is a valid ObjectId) - is appalling to say the least. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
What do I expect by dumping out this long post? Thank you all for you work, sorry if anything sounds harsh. |
Beta Was this translation helpful? Give feedback.
-
See #11227 for conclusion: it all resulted in changes made to code
|
Beta Was this translation helpful? Give feedback.
See #11227 for conclusion: it all resulted in changes made to code