Skip to content

Commit 8838dfc

Browse files
committed
write test, begin debug
1 parent a57bb72 commit 8838dfc

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

lib/cast.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ module.exports = function cast(schema, obj, options, context) {
6161
val = obj[path];
6262

6363
if (path === '$or' || path === '$nor' || path === '$and') {
64+
console.log('what is val', val);
6465
if (!Array.isArray(val)) {
6566
throw new CastError('Array', val, path);
6667
}
@@ -303,7 +304,6 @@ module.exports = function cast(schema, obj, options, context) {
303304
} else {
304305
const ks = Object.keys(val);
305306
let $cond;
306-
307307
let k = ks.length;
308308

309309
while (k--) {
@@ -367,6 +367,8 @@ module.exports = function cast(schema, obj, options, context) {
367367

368368
obj[path] = { $in: casted };
369369
} else {
370+
console.log('Houston we have a problem.');
371+
console.log('what is obj[path]', obj[path], path, obj, val);
370372
obj[path] = schematype.castForQuery(
371373
null,
372374
val,

lib/schematype.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,7 @@ SchemaType.prototype._applySetters = function(value, scope, init, priorVal, opti
11861186
const setters = this.setters;
11871187

11881188
for (let i = setters.length - 1; i >= 0; i--) {
1189+
console.log('what is v', v, setters.length);
11891190
v = setters[i].call(scope, v, priorVal, this, options);
11901191
}
11911192

@@ -1215,7 +1216,6 @@ SchemaType.prototype.applySetters = function(value, scope, init, priorVal, optio
12151216
if (v == null) {
12161217
return this._castNullish(v);
12171218
}
1218-
12191219
// do not cast until all setters are applied #665
12201220
v = this.cast(v, scope, init, priorVal, options);
12211221

test/model.discriminator.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,4 +2180,34 @@ describe('model', function() {
21802180
assert.ok(innerBuildingsPath.schemaOptions.type.discriminators.Garage);
21812181
assert.equal(innerBuildingsPath.schemaOptions.type.discriminators.Garage.discriminatorMapping.value, 'G');
21822182
});
2183+
it('should not fail when using a discriminator key multiple times (gh-13906)', async function() {
2184+
const options = { discriminatorKey: 'type' };
2185+
const eventSchema = new Schema({ date: Schema.Types.Date }, options);
2186+
const Event = db.model('gh-13906-event', eventSchema);
2187+
2188+
2189+
const clickedLinkEventSchema = new Schema({ url: String }, options);
2190+
const ClickedLinkEvent = Event.discriminator('ClickedLinkEvent', clickedLinkEventSchema, 'clickedLinkEvent');
2191+
2192+
2193+
const clickedImageEventSchema = new Schema({ image: String }, options);
2194+
const ClickedImageEvent = Event.discriminator('ClickedImageEvent', clickedImageEventSchema, 'clickedImageEvent');
2195+
2196+
const clickedLinkEvent = new ClickedLinkEvent({ url: 'https://clicked-link.com' });
2197+
assert.equal(clickedLinkEvent.type, 'clickedLinkEvent');
2198+
assert.equal(clickedLinkEvent.url, 'https://clicked-link.com');
2199+
2200+
const clickedImageEvent = new ClickedImageEvent({ image: 'clicked-image.png' });
2201+
assert.equal(clickedImageEvent.type, 'clickedImageEvent');
2202+
assert.equal(clickedImageEvent.image, 'clicked-image.png');
2203+
const query = {
2204+
type: 'clickedLinkEvent',
2205+
$or: [
2206+
{ type: 'clickedImageEvent' }
2207+
]
2208+
};
2209+
const result = await Event.find(query).exec();
2210+
assert(result);
2211+
2212+
});
21832213
});

0 commit comments

Comments
 (0)