Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Island rhythms/gh 13906 handle changing discrim key #13938

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

});
});
2 changes: 1 addition & 1 deletion test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function gh13930() {

const Test = connection.model<ITest>('Test', TestSchema);

Test.insertMany<{foo: string}>([{ foo: 'bar' }], { });
Test.insertMany<{ foo: string }>([{ foo: 'bar' }], { });
}

function gh10074() {
Expand Down