Skip to content

Commit

Permalink
fix: correctly apply schema-level discriminators when compiling model…
Browse files Browse the repository at this point in the history
… re: #7971
  • Loading branch information
vkarpov15 committed Apr 14, 2022
1 parent 10feddb commit 5eeb582
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
3 changes: 1 addition & 2 deletions lib/helpers/model/discriminator.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu

utils.merge(schema, baseSchema, {
isDiscriminatorSchemaMerge: true,
omit: { discriminators: true, base: true },
omit: { discriminators: true, base: true, _applyDiscriminators: true },
omitNested: conflictingPaths.reduce((cur, path) => {
cur['tree.' + path] = true;
return cur;
Expand Down Expand Up @@ -140,7 +140,6 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
obj[key][schema.options.typeKey] = existingPath ? existingPath.options[schema.options.typeKey] : String;
schema.add(obj);


schema.discriminatorMapping = { key: key, value: value, isRoot: false };

if (baseSchema.options.collection) {
Expand Down
10 changes: 6 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,6 @@ Mongoose.prototype.model = function(name, schema, collection, options) {
}

const model = _mongoose._model(name, schema, collection, options);
for (const disc in schema._applyDiscriminators) {
model.discriminator(disc, schema._applyDiscriminators[disc]);
}
_mongoose.connection.models[name] = model;
_mongoose.models[name] = model;

Expand All @@ -536,7 +533,6 @@ Mongoose.prototype.model = function(name, schema, collection, options) {
*/

Mongoose.prototype._model = function(name, schema, collection, options) {

const _mongoose = this instanceof Mongoose ? this : mongoose;

let model;
Expand Down Expand Up @@ -572,6 +568,12 @@ Mongoose.prototype._model = function(name, schema, collection, options) {

connection.emit('model', model);

if (schema._applyDiscriminators != null) {
for (const disc of Object.keys(schema._applyDiscriminators)) {
model.discriminator(disc, schema._applyDiscriminators[disc]);
}
}

return model;
};

Expand Down
7 changes: 5 additions & 2 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ Schema.prototype._clone = function _clone(Constructor) {
if (this.discriminators != null) {
s.discriminators = Object.assign({}, this.discriminators);
}
if (this._applyDiscriminators != null) {
s._applyDiscriminators = Object.assign({}, this._applyDiscriminators);
}

s.aliases = Object.assign({}, this.aliases);

Expand Down Expand Up @@ -470,9 +473,9 @@ Schema.prototype.defaultOptions = function(options) {
return options;
};

Schema.prototype.discriminator = function(name, obj) {
Schema.prototype.discriminator = function(name, schema) {
this._applyDiscriminators = {};
this._applyDiscriminators[name] = obj;
this._applyDiscriminators[name] = schema;
};

/**
Expand Down
11 changes: 7 additions & 4 deletions test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2732,32 +2732,35 @@ describe('schema', function() {
const foundUser = await User.findOne({ _id: user._id });
assert.ok(Array.isArray(foundUser.data));
});
it('sets an _applyDiscriminators property on the schema and add discriminator to appropriate model gh-7971-p2', async() => {

it('sets an _applyDiscriminators property on the schema and add discriminator to appropriate model (gh-7971)', async() => {
const eventSchema = new mongoose.Schema({ message: String },
{ discriminatorKey: 'kind', _id: false });
{ discriminatorKey: 'kind' });
const batchSchema = new mongoose.Schema({ name: String }, { discriminatorKey: 'kind' });
batchSchema.discriminator('event', eventSchema);
assert(batchSchema._applyDiscriminators);
assert.ok(!eventSchema._applyDiscriminators);

const arraySchema = new mongoose.Schema({ array: [batchSchema] });
const arrayModel = db.model('array', arraySchema);
const array = await arrayModel.create({
array: [{ name: 'Array Test', message: 'Please work', kind: 'event' }]
});
console.log(array);
assert(array.array[0].message);

const parentSchema = new mongoose.Schema({ sub: batchSchema });
const Parent = db.model('Parent', parentSchema);
const parent = await Parent.create({
sub: { name: 'Sub Test', message: 'I hope I worked!', kind: 'event' }
});
assert(parent.sub.message);

const Batch = db.model('Batch', batchSchema);
const batch = await Batch.create({
name: 'Test Testerson',
message: 'Hello World!',
kind: 'event'
});
console.log('batch', batch, batch.message);
assert(batch.message);
});
});

0 comments on commit 5eeb582

Please sign in to comment.