Skip to content

Commit

Permalink
Merge pull request #13938 from Automattic/IslandRhythms/gh-13906
Browse files Browse the repository at this point in the history
Island rhythms/gh 13906 handle changing discrim key
  • Loading branch information
vkarpov15 committed Oct 5, 2023
2 parents d5964cd + 29aa4fc commit 18d5d79
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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 @@ -303,7 +309,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
1 change: 0 additions & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4889,7 +4889,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;
if (obj != null &&
Expand Down
1 change: 0 additions & 1 deletion lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,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
30 changes: 30 additions & 0 deletions test/model.discriminator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2180,4 +2180,34 @@ describe('model', function() {
assert.ok(innerBuildingsPath.schemaOptions.type.discriminators.Garage);
assert.equal(innerBuildingsPath.schemaOptions.type.discriminators.Garage.discriminatorMapping.value, 'G');
});
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 18d5d79

Please sign in to comment.