Skip to content

Commit

Permalink
Merge pull request #40 from DesignByOnyx/39-non-discriminated-documents
Browse files Browse the repository at this point in the history
fix: allow non-discriminated documents be retrieved
  • Loading branch information
vkarpov15 authored Aug 4, 2024
2 parents b1b7265 + aa5bf99 commit d27185f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ function getSchemaForDoc(schema, res) {
break;
}
}
return childSchema;

// If no discriminator schema found, return the root schema (#39)
return childSchema || schema;
}

function applyGettersToDoc(schema, doc, fields, prefix) {
Expand Down
14 changes: 14 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,18 @@ describe('mongoose-lean-getters', function() {
assert.equal(typeof doc.field, 'string');
assert.strictEqual(doc.field, '1337');
});

it('should allow non-discriminated documents to be retrieved (#39)', async() => {
const baseSchema = new mongoose.Schema({ foo: String });
baseSchema.plugin(mongooseLeanGetters);

const BaseModel = mongoose.model('BaseModel-gh-39', baseSchema);
BaseModel.discriminator('Custom', new mongoose.Schema({}));

// Simply retrieving the non-discriminated document causes the error
await assert.doesNotReject(async() => {
const entry = await BaseModel.create({ foo: 'foo' });
await BaseModel.findById(entry._id).lean({ getters: true });
});
});
});

0 comments on commit d27185f

Please sign in to comment.