Skip to content

Commit

Permalink
Fixed handling discriminators in nested document arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-knight committed Mar 19, 2024
1 parent 87eb2ca commit b632552
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function getSchemaForDoc(schema, res) {
const discriminatorValue = res[schema.discriminatorMapping.key];
let childSchema = undefined;
for (const name of Object.keys(schema.discriminators)) {
const matchValue = schema.discriminators[name].discriminatorMapping.value || modelName;
const matchValue = schema.discriminators[name].discriminatorMapping.value;
if (matchValue === discriminatorValue) {
childSchema = schema.discriminators[name];
break;
Expand All @@ -91,7 +91,13 @@ function applyGettersToDoc(schema, doc, fields, prefix) {
if (Array.isArray(doc)) {
for (let i = 0; i < doc.length; ++i) {
const currentDoc = doc[i];
// If the current doc is null/undefined, there's nothing to do
if (currentDoc == null) continue;
// If it is a nested array, apply getters to each subdocument (otherwise it would attempt to apply getters to the array itself)
if (Array.isArray(currentDoc)) {
applyGettersToDoc.call(this, schema, currentDoc, fields, prefix);
continue;
}
const schemaForDoc = getSchemaForDoc(schema, currentDoc);
applyGettersToDoc.call(this, schemaForDoc, currentDoc, fields, prefix);
}
Expand Down
38 changes: 36 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe('mongoose-lean-getters', function() {

assert.equal(docs[0].url, 'https://www.test.com discriminator field');
});

it('should call getters on schemas with discriminator using explicit value', async function() {
const options = { discriminatorKey: 'kind' };

Expand Down Expand Up @@ -304,7 +304,41 @@ describe('mongoose-lean-getters', function() {

assert.equal(docs[0].events[0].url, 'https://www.test.com discriminator field');
});


it('should work on schemas with discriminators in nested arrays', async function() {
const options = { discriminatorKey: 'kind' };

const eventSchema = new mongoose.Schema({ time: Date }, options);
const clickedLinkSchema = new mongoose.Schema({
url: { type: String, get: v => v + ' discriminator field' }
});
eventSchema.discriminator('VisitedPage', clickedLinkSchema);

const eventGroupSchema = new mongoose.Schema({
events: [eventSchema]
});

const eventListSchema = new mongoose.Schema({
eventGroups: [eventGroupSchema],
});
eventListSchema.plugin(mongooseLeanGetters);
const EventGroupList = mongoose.model('EventGroupList', eventListSchema);

await EventGroupList.deleteMany({});
await EventGroupList.create({
eventGroups: [{
events: [{
kind: 'VisitedPage',
url: 'https://www.test.com'
}],
}],
});

const docs = await EventGroupList.find().lean({ getters: true });

assert.equal(docs[0].eventGroups[0].events[0].url, 'https://www.test.com discriminator field');
});

it('should call getters on arrays (gh-30)', async function() {
function upper(value) {
return value.toUpperCase();
Expand Down

0 comments on commit b632552

Please sign in to comment.