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

Document.prototype.toObject() should run all getters on fields in subdocuments when getters option is set to true #14835

Closed
2 tasks done
lcrosetto opened this issue Aug 28, 2024 · 2 comments · Fixed by #14844
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@lcrosetto
Copy link

lcrosetto commented Aug 28, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.5.4

Node.js version

20.17.0

MongoDB server version

7.0.11

Typescript version (if applicable)

No response

Description

In versions 8.5.0 and prior, a call to Document.prototype.toObject() would run getters defined in schemas for subdocuments of arbitrary depth. In 8.5.1 and subsequent versions, toObject() will not run getters for subdocuments nested more than 1 level deep.

Getters should be run on all subdocument fields.

Steps to Reproduce

The following code will run the getters on field1 and field2 in mongoose versions 8.5.0 and prior, and will not run the getter for field2 in 8.5.1 and subsequent versions:

      const embed2Schema = new mongoose.Schema({
        field2: {
          type: Date,
          get: (v) => v.toISOString().slice(0, 10),
        },
      }, {_id: false, toObject: {getters: true}});
      const embed1Schema = new mongoose.Schema({
        subdoc: embed2Schema,
        field1: {
          type: Date,
          get: (v) => v.toISOString().slice(0, 10),
        },
      }, {_id: false, toObject: {getters: true}});
      const testSchema = new mongoose.Schema({
        testArray: [embed1Schema],
      });
      const TestModel = mongoose.model('Test', testSchema);
      const d = TestModel.hydrate({
        testArray: [{field1: '2022-02-01', subdoc: {field2: '2022-02-01'}}],
      });
      const o = d.toObject();
      const {field1} = o.testArray[0];
      const {field2} = o.testArray[0].subdoc;
      if (field1 !== field2) throw new Error(`field1: ${field1} field2: ${field2}`);

The above test will succeed on 8.5.0 and previous, and fail on 8.5.1 and subsequent versions.

Expected Behavior

All defined getters should be run for nested subdocuments.

@vkarpov15 vkarpov15 added this to the 8.6.1 milestone Aug 28, 2024
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Aug 28, 2024
@vkarpov15
Copy link
Collaborator

Potentially the same issue as #14840

@lukas-becker0
Copy link

This seem to be the same issue I encountered, in my case it also started with mongoose >= 8.5.1
8.5.0 and prior work as expected.

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
3 participants