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

Fixed handling discriminators in nested document arrays #32

Merged
merged 1 commit into from
Apr 27, 2024
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
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
Loading