mongoosejs / mongoose-lean-getters

Apply getters on lean() documents: https://mongoosejs.com/docs/tutorials/lean.html
Apache License 2.0
10 stars 15 forks source link

Does not work on nested schemas on discriminated models #42

Open DesignByOnyx opened 3 months ago

DesignByOnyx commented 3 months ago

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

We have a shared schema which gets referenced by several models. Whenever this schema is part of a discriminated model, the getters do not get called for lean queries.

If the current behavior is a bug, please provide the steps to reproduce.

const nestedSchema = new mongoose.Schema({
  num: {
    type: mongoose.Types.Decimal128,
    get: (val) => `${val}`
  }
});

const rootSchema = new mongoose.Schema({});
rootSchema.plugin(mongooseLeanGetters);

const discriminatedSchema = new mongoose.Schema({
  discriminatedProp: { type: nestedSchema },
  discriminatedArray: { type: [nestedSchema] },
});

const RootModel = mongoose.model('Root', rootSchema);
const DiscriminatedModel = RootModel.discriminator('Discriminated', discriminatedSchema);

const entry = await DiscriminatedModel.create({
  discriminatedProp: { num: -0.222222222222222222 },
  discriminatedArray: [{ num: -0.333333333333333333 }],
});

const found = await DiscriminatedModel.findById(entry._id).lean({ getters: true }).exec();

assert.equal(typeof found.discriminatedProp.num, 'string');  // fail
assert.equal(typeof found.discriminatedArray[0].num, 'string');  // fail

What is the expected behavior?

The getters should be called

What are the versions of Node.js, mongoose-lean-getters, and Mongoose are you are using? Note that "latest" is not a version.

Node: v20.12.2 mongoose: v8.5.0 (also v7.7.0) mongoose-lean-getters: v2.1.0