Skip to content

Commit

Permalink
fix: handle changing discrim key on nested ops for GH issue Automatti…
Browse files Browse the repository at this point in the history
  • Loading branch information
Koen Lienaerts committed Oct 11, 2023
1 parent d11bd18 commit f7f8f30
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ module.exports = function cast(schema, obj, options, context) {
if (val[k] == null || typeof val[k] !== 'object') {
throw new CastError('Object', val[k], path + '.' + k);
}
val[k] = cast(schema, val[k], options, context);
const discriminatorValue = val[k][schema.options.discriminatorKey];
if (discriminatorValue == null) {
val[k] = cast(schema, val[k], options, context);
} else {
const discriminatorSchema = getSchemaDiscriminatorByValue(context.schema, discriminatorValue);
val[k] = cast(discriminatorSchema ? discriminatorSchema : schema, val[k], options, context);
}
}
} else if (path === '$where') {
type = typeof val;
Expand Down Expand Up @@ -278,7 +284,6 @@ module.exports = function cast(schema, obj, options, context) {
} else {
const ks = Object.keys(val);
let $cond;

let k = ks.length;

while (k--) {
Expand Down Expand Up @@ -361,4 +366,4 @@ function _cast(val, numbertype, context) {
}
}
}
}
}
1 change: 0 additions & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4746,7 +4746,6 @@ function _getPopulatedPaths(list, arr, prefix) {

Query.prototype.cast = function(model, obj) {
obj || (obj = this._conditions);

model = model || this.model;

const discriminatorKey = model.schema.options.discriminatorKey;
Expand Down
1 change: 0 additions & 1 deletion lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,6 @@ SchemaType.prototype.applySetters = function(value, scope, init, priorVal, optio
if (v == null) {
return this._castNullish(v);
}

// do not cast until all setters are applied #665
v = this.cast(v, scope, init, priorVal, options);

Expand Down
31 changes: 31 additions & 0 deletions test/model.discriminator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1645,4 +1645,35 @@ describe('model', function() {
actions.discriminator('message', Message.schema);
assert.ok(actions.schema.discriminators['message']);
});

it('should not fail when using a discriminator key multiple times (gh-13906)', async function() {
const options = { discriminatorKey: 'type' };
const eventSchema = new Schema({ date: Schema.Types.Date }, options);
const Event = db.model('gh-13906-event', eventSchema);


const clickedLinkEventSchema = new Schema({ url: String }, options);
const ClickedLinkEvent = Event.discriminator('ClickedLinkEvent', clickedLinkEventSchema, 'clickedLinkEvent');


const clickedImageEventSchema = new Schema({ image: String }, options);
const ClickedImageEvent = Event.discriminator('ClickedImageEvent', clickedImageEventSchema, 'clickedImageEvent');

const clickedLinkEvent = new ClickedLinkEvent({ url: 'https://clicked-link.com' });
assert.equal(clickedLinkEvent.type, 'clickedLinkEvent');
assert.equal(clickedLinkEvent.url, 'https://clicked-link.com');

const clickedImageEvent = new ClickedImageEvent({ image: 'clicked-image.png' });
assert.equal(clickedImageEvent.type, 'clickedImageEvent');
assert.equal(clickedImageEvent.image, 'clicked-image.png');
const query = {
type: 'clickedLinkEvent',
$or: [
{ type: 'clickedImageEvent' }
]
};
const result = await Event.find(query).exec();
assert(result);

});
});

0 comments on commit f7f8f30

Please sign in to comment.