Skip to content

Commit

Permalink
fix(discriminator): don't copy discriminators property from base sc…
Browse files Browse the repository at this point in the history
…hema

Fix #6064
  • Loading branch information
vkarpov15 committed Feb 12, 2018
1 parent 0290187 commit 36b2f3f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/services/model/discriminator.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ module.exports = function discriminator(model, name, schema) {
delete schema.paths._id;
delete schema.tree._id;
}
utils.merge(schema, baseSchema, { retainKeyOrder: true });
utils.merge(schema, baseSchema, {
retainKeyOrder: true,
omit: { discriminators: true }
});

var obj = {};
obj[key] = {
Expand Down
6 changes: 6 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@ exports.merge = function merge(to, from, options) {
if (options.retainKeyOrder) {
while (i < len) {
key = keys[i++];
if (options.omit && options.omit[key]) {
continue;
}
if (to[key] == null) {
to[key] = from[key];
} else if (exports.isObject(from[key])) {
Expand All @@ -429,6 +432,9 @@ exports.merge = function merge(to, from, options) {
} else {
while (len--) {
key = keys[len];
if (options.omit && options.omit[key]) {
continue;
}
if (to[key] == null) {
to[key] = from[key];
} else if (exports.isObject(from[key])) {
Expand Down
75 changes: 75 additions & 0 deletions test/helpers/populate.getSchemaTypes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,79 @@ describe('getSchemaTypes', function() {

done();
});

it('multiple embedded discriminators (gh-6064)', function(done) {
var ItemSchema = new Schema({
title: {
type: String,
required: true
}
}, {discriminatorKey: 'type'});

var ItemBookSchema = new Schema({
author: {
type: String,
ref: 'Ref1'
}
});

var ItemEBookSchema = new Schema({
author: {
type: String,
ref: 'Ref2'
}
});

var OtherItem = new Schema({ name: String });

var BundleSchema = new Schema({
level1: {
items: [{
type: ItemSchema,
required: false
}]
},
level2: {
items: [{
type: ItemSchema,
required: false
}]
}
});

BundleSchema.path('level1.items').discriminator('Book', ItemBookSchema);
BundleSchema.path('level1.items').discriminator('EBook', ItemEBookSchema);
BundleSchema.path('level1.items').discriminator('Other', OtherItem);

// HERE IS THE ADDED DISCRIMINATOR PATH
BundleSchema.path('level2.items').discriminator('Book', ItemBookSchema);
BundleSchema.path('level2.items').discriminator('EBook', ItemEBookSchema);
BundleSchema.path('level2.items').discriminator('Other', OtherItem);

var doc = {
level1: {
items: [
{ type: 'Book', author: 'level 1 test 1' },
{ type: 'EBook', author: 'level 1 test 2' }
]
},
level2: {
items: [
{ type: 'EBook', author: 'level 2 test 1' },
{ type: 'Book', author: 'level 2 test 2' },
{ type: 'Other' }
]
}
};
var schemaTypes = getSchemaTypes(BundleSchema, doc, 'level2.items.author');

assert.ok(Array.isArray(schemaTypes));
// Make sure we only got the schema paths for Book and EBook, and none
// for the 'Other'
assert.equal(schemaTypes.length, 2);
assert.equal(schemaTypes[0].options.ref, 'Ref1');
assert.equal(schemaTypes[1].options.ref, 'Ref2');

done();
});
});

0 comments on commit 36b2f3f

Please sign in to comment.