Skip to content

Commit

Permalink
write test, begin debug
Browse files Browse the repository at this point in the history
  • Loading branch information
IslandRhythms committed Oct 4, 2023
1 parent a57bb72 commit 8838dfc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = function cast(schema, obj, options, context) {
val = obj[path];

if (path === '$or' || path === '$nor' || path === '$and') {
console.log('what is val', val);
if (!Array.isArray(val)) {
throw new CastError('Array', val, path);
}
Expand Down Expand Up @@ -303,7 +304,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 @@ -367,6 +367,8 @@ module.exports = function cast(schema, obj, options, context) {

obj[path] = { $in: casted };
} else {
console.log('Houston we have a problem.');
console.log('what is obj[path]', obj[path], path, obj, val);
obj[path] = schematype.castForQuery(
null,
val,
Expand Down
2 changes: 1 addition & 1 deletion lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ SchemaType.prototype._applySetters = function(value, scope, init, priorVal, opti
const setters = this.setters;

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

Expand Down Expand Up @@ -1215,7 +1216,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 8838dfc

Please sign in to comment.