Skip to content

Commit 3f1f1de

Browse files
committed
fix: remove discriminator schema _id before merging if base schema has custom _id without calling merge()
Fix #8546 Re: #8543
1 parent c91c1a4 commit 3f1f1de

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

lib/helpers/model/discriminator.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
7272
if (baseSchema.paths._id &&
7373
baseSchema.paths._id.options &&
7474
!baseSchema.paths._id.options.auto) {
75-
delete schema.paths._id;
76-
delete schema.tree._id;
75+
schema.remove('_id');
7776
}
7877

7978
// Find conflicting paths: if something is a path in the base schema
@@ -88,7 +87,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
8887
}
8988

9089
utils.merge(schema, baseSchema, {
91-
omit: { discriminators: true },
90+
omit: { discriminators: true, base: true },
9291
omitNested: conflictingPaths.reduce((cur, path) => {
9392
cur['tree.' + path] = true;
9493
return cur;

test/model.discriminator.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ describe('model', function() {
13331333
const model = mongoose.model('Model', new mongoose.Schema({ _id: Number }));
13341334
const passedInSchema = new mongoose.Schema({});
13351335
model.discriminator('Discrimintaor', passedInSchema);
1336-
assert.equal(passedInSchema.path('_id').instance, 'ObjectID');
1336+
assert.equal(passedInSchema.path('_id').instance, 'Number');
13371337
});
13381338

13391339
function throwErrorOnClone() { throw new Error('clone() was called on the unrelated schema'); };

0 commit comments

Comments
 (0)