Skip to content

Commit

Permalink
fix; ability to customize _id field for discriminators (Re: #3482)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Nov 3, 2015
1 parent f3b837c commit 51c14f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,19 +811,25 @@ Model.discriminator = function discriminator(name, schema) {
delete a.toObject;
delete b.toJSON;
delete b.toObject;
delete a._id;
delete b._id;

if (!utils.deepEqual(a, b)) {
throw new Error("Discriminator options are not customizable " +
"(except toJSON & toObject)");
"(except toJSON, toObject, _id)");
}
})(schema.options, baseSchema.options);

var toJSON = schema.options.toJSON,
toObject = schema.options.toObject;
var toJSON = schema.options.toJSON;
var toObject = schema.options.toObject;
var _id = schema.options._id;

schema.options = utils.clone(baseSchema.options);
if (toJSON) schema.options.toJSON = toJSON;
if (toObject) schema.options.toObject = toObject;
if (typeof _id !== 'undefined') {
schema.options._id = _id;
}

schema.callQueue = baseSchema.callQueue.concat(schema.callQueue.slice(schema._defaultMiddleware.length));
schema._requiredpaths = undefined; // reset just in case Schema#requiredPaths() was called on either schema
Expand Down
2 changes: 1 addition & 1 deletion test/model.discriminator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ describe('model', function() {
errorMessage = e.message;
}

assert.equal(errorMessage, 'Discriminator options are not customizable (except toJSON & toObject)');
assert.equal(errorMessage, 'Discriminator options are not customizable (except toJSON, toObject, _id)');
done();
});
});
Expand Down

0 comments on commit 51c14f6

Please sign in to comment.