Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ Document.prototype.$init = function() {
*/

Document.prototype.$__init = function(doc, opts) {
if (doc == null) {
throw new ObjectParameterError(doc, 'doc', 'init');
}
this.$isNew = false;
opts = opts || {};

Expand Down
72 changes: 49 additions & 23 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const ObjectId = Schema.Types.ObjectId;
const DocumentObjectId = mongoose.Types.ObjectId;
const EmbeddedDocument = mongoose.Types.Subdocument;
const MongooseError = mongoose.Error;
const ObjectParameterError = require('../lib/error/objectParameter');

describe('Model', function() {
let db;
Expand Down Expand Up @@ -369,7 +370,7 @@ describe('Model', function() {
assert.equal(post.get('comments')[0].comments[0].isNew, true);
post.invalidate('title'); // force error

await post.save().catch(() => {});
await post.save().catch(() => { });
assert.equal(post.isNew, true);
assert.equal(post.get('comments')[0].isNew, true);
assert.equal(post.get('comments')[0].comments[0].isNew, true);
Expand Down Expand Up @@ -2476,7 +2477,7 @@ describe('Model', function() {

const DefaultErr = db.model('Test', DefaultErrSchema);

new DefaultErr().save().catch(() => {});
new DefaultErr().save().catch(() => { });

await new Promise(resolve => {
DefaultErr.once('error', function(err) {
Expand Down Expand Up @@ -3040,7 +3041,7 @@ describe('Model', function() {
const Location = db.model('Test', LocationSchema);


await Location.collection.drop().catch(() => {});
await Location.collection.drop().catch(() => { });
await Location.init();

await Location.create({
Expand Down Expand Up @@ -3509,7 +3510,7 @@ describe('Model', function() {
listener = null;
// Change stream may still emit "MongoAPIError: ChangeStream is closed" because change stream
// may still poll after close.
changeStream.on('error', () => {});
changeStream.on('error', () => { });
changeStream.close();
changeStream = null;
});
Expand Down Expand Up @@ -3661,7 +3662,7 @@ describe('Model', function() {

// Change stream may still emit "MongoAPIError: ChangeStream is closed" because change stream
// may still poll after close.
changeStream.on('error', () => {});
changeStream.on('error', () => { });
await changeStream.close();
await db.close();
});
Expand All @@ -3679,7 +3680,7 @@ describe('Model', function() {

// Change stream may still emit "MongoAPIError: ChangeStream is closed" because change stream
// may still poll after close.
changeStream.on('error', () => {});
changeStream.on('error', () => { });

const close = changeStream.close();
await db.asPromise();
Expand All @@ -3705,7 +3706,7 @@ describe('Model', function() {

// Change stream may still emit "MongoAPIError: ChangeStream is closed" because change stream
// may still poll after close.
changeStream.on('error', () => {});
changeStream.on('error', () => { });

changeStream.close();
const closedData = await closed;
Expand Down Expand Up @@ -5570,7 +5571,7 @@ describe('Model', function() {
const Model = db.model('User', userSchema);


await Model.collection.drop().catch(() => {});
await Model.collection.drop().catch(() => { });
await Model.createCollection();
const collectionName = Model.collection.name;

Expand Down Expand Up @@ -5604,7 +5605,7 @@ describe('Model', function() {
const Test = db.model('Test', schema, 'Test');
await Test.init();

await Test.collection.drop().catch(() => {});
await Test.collection.drop().catch(() => { });
await Test.createCollection();

const collections = await Test.db.db.listCollections().toArray();
Expand All @@ -5613,7 +5614,7 @@ describe('Model', function() {
assert.equal(coll.type, 'timeseries');
assert.equal(coll.options.timeseries.timeField, 'timestamp');

await Test.collection.drop().catch(() => {});
await Test.collection.drop().catch(() => { });
});

it('createCollection() enforces expireAfterSeconds (gh-11229)', async function() {
Expand All @@ -5634,7 +5635,7 @@ describe('Model', function() {

const Test = db.model('TestGH11229Var1', schema);

await Test.collection.drop().catch(() => {});
await Test.collection.drop().catch(() => { });
await Test.createCollection({ expireAfterSeconds: 5 });

const collOptions = await Test.collection.options();
Expand Down Expand Up @@ -5662,7 +5663,7 @@ describe('Model', function() {

const Test = db.model('TestGH11229Var2', schema, 'TestGH11229Var2');

await Test.collection.drop().catch(() => {});
await Test.collection.drop().catch(() => { });
await Test.createCollection({ expires: '5 seconds' });

const collOptions = await Test.collection.options();
Expand Down Expand Up @@ -5690,7 +5691,7 @@ describe('Model', function() {

const Test = db.model('TestGH11229Var3', schema);

await Test.collection.drop().catch(() => {});
await Test.collection.drop().catch(() => { });
await Test.createCollection();

const collOptions = await Test.collection.options();
Expand Down Expand Up @@ -5718,7 +5719,7 @@ describe('Model', function() {

const Test = db.model('TestGH11229Var4', schema);

await Test.collection.drop().catch(() => {});
await Test.collection.drop().catch(() => { });
await Test.createCollection();

const collOptions = await Test.collection.options();
Expand Down Expand Up @@ -5746,7 +5747,7 @@ describe('Model', function() {
const Test = db.model('Test', schema, 'Test');
await Test.init();

await Test.collection.drop().catch(() => {});
await Test.collection.drop().catch(() => { });
await Test.createCollection();

const collections = await Test.db.db.listCollections().toArray();
Expand All @@ -5755,7 +5756,7 @@ describe('Model', function() {
assert.deepEqual(coll.options.clusteredIndex.key, { _id: 1 });
assert.equal(coll.options.clusteredIndex.name, 'clustered test');

await Test.collection.drop().catch(() => {});
await Test.collection.drop().catch(() => { });
});

it('mongodb actually removes expired documents (gh-11229)', async function() {
Expand All @@ -5777,7 +5778,7 @@ describe('Model', function() {

const Test = db.model('TestMongoDBExpireRemoval', schema);

await Test.collection.drop().catch(() => {});
await Test.collection.drop().catch(() => { });
await Test.createCollection({ expireAfterSeconds: 5 });

await Test.insertMany([
Expand Down Expand Up @@ -5875,7 +5876,7 @@ describe('Model', function() {
const Model = db.model('User', userSchema);


await Model.collection.drop().catch(() => {});
await Model.collection.drop().catch(() => { });

await Model.createCollection();
await Model.createCollection();
Expand Down Expand Up @@ -6557,7 +6558,7 @@ describe('Model', function() {
await User.bulkWrite([
{
updateOne: {
filter: { },
filter: {},
update: { friends: ['Sam'] },
upsert: true,
setDefaultsOnInsert: true
Expand Down Expand Up @@ -7326,7 +7327,7 @@ describe('Model', function() {
});

it('insertMany should throw an error if there were operations that failed validation, ' +
'but all operations that passed validation succeeded (gh-14572) (gh-13256)', async function() {
'but all operations that passed validation succeeded (gh-14572) (gh-13256)', async function() {
const userSchema = new Schema({
age: { type: Number }
});
Expand Down Expand Up @@ -8488,7 +8489,7 @@ describe('Model', function() {
decoratorSchema.loadClass(Decorator);

// Define discriminated class before model is compiled
class Deco1 extends Decorator { whoAmI() { return 'I am Test1'; }}
class Deco1 extends Decorator { whoAmI() { return 'I am Test1'; } }
const deco1Schema = new Schema({});
deco1Schema.loadClass(Deco1);
decoratorSchema.discriminator('Test1', deco1Schema);
Expand All @@ -8500,7 +8501,7 @@ describe('Model', function() {
const shopModel = db.model('Test', shopSchema);

// Define another discriminated class after the model is compiled
class Deco2 extends Decorator { whoAmI() { return 'I am Test2'; }}
class Deco2 extends Decorator { whoAmI() { return 'I am Test2'; } }
const deco2Schema = new Schema({});
deco2Schema.loadClass(Deco2);
decoratorSchema.discriminator('Test2', deco2Schema);
Expand Down Expand Up @@ -8627,7 +8628,7 @@ describe('Model', function() {
});

it('insertMany should throw an error if there were operations that failed validation, ' +
'but all operations that passed validation succeeded (gh-13256)', async function() {
'but all operations that passed validation succeeded (gh-13256)', async function() {
const userSchema = new Schema({
age: { type: Number }
});
Expand Down Expand Up @@ -9164,6 +9165,31 @@ describe('Model', function() {
assert.strictEqual(doc.name, 'Test2');
});
});
describe('gh-15812', function() {
it('should throw ObjectParameterError when init is called with null', function() {
const doc = new mongoose.Document({}, new mongoose.Schema({ name: String }));
try {
doc.init(null);
assert.fail('Should have thrown an error');
} catch (error) {
assert.ok(error instanceof ObjectParameterError);
assert.strictEqual(error.name, 'ObjectParameterError');
assert.ok(error.message.includes('Parameter "doc" to init() must be an object'));
}
});

it('should throw ObjectParameterError when init is called with undefined', function() {
const doc = new mongoose.Document({}, new mongoose.Schema({ name: String }));
try {
doc.init(undefined);
assert.fail('Should have thrown an error');
} catch (error) {
assert.ok(error instanceof ObjectParameterError);
assert.strictEqual(error.name, 'ObjectParameterError');
assert.ok(error.message.includes('Parameter "doc" to init() must be an object'));
}
});
});
});


Expand Down