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

toJSON options gets passed to implicitly created schemas, including transform #13599

Closed
2 tasks done
ksmithut opened this issue Jul 10, 2023 · 1 comment · Fixed by #13634
Closed
2 tasks done

toJSON options gets passed to implicitly created schemas, including transform #13599

ksmithut opened this issue Jul 10, 2023 · 1 comment · Fixed by #13634
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ksmithut
Copy link

ksmithut commented Jul 10, 2023

Prerequisites

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

Mongoose version

7.2.0+

Node.js version

20.x

MongoDB server version

6.x

Typescript version (if applicable)

No response

Description

A new feature was added (#13325) where toJSON and toObject options get passed to implicitly created schemas in a schema. I believe this was so that if you set virtuals: true and getters: true that those options would get passed. Another option gets passed that gives us unexpected results: transform. We use this transform() function to modify the document before it goes out. Before it was only being run for the top level schema, but now it gets run for every sub document.

Steps to Reproduce

import mongoose from 'mongoose'

const schema = new mongoose.Schema(
  {
    items: [{ key: String }]
  },
  {
    toJSON: {
      transform (doc, obj) {
        console.log(obj)
        return obj
      }
    }
  }
)
const MyModel = mongoose.model('MyModel', schema)
await mongoose.connect('mongodb://localhost:27017/testing')
const doc = await MyModel.create({
  items: [{ key: 'foobar' }]
})
doc.toJSON()
await mongoose.disconnect()

This will produce logs that look something like this:

{ key: 'foobar', _id: new ObjectId("[objectid]") }
{
  items: [ { key: 'foobar', _id: new ObjectId("[objectid]") } ],
  _id: new ObjectId("[objectid]"),
  __v: 0
}

Expected Behavior

Expected behavior is that the defined transform function would only apply to the schema on which it is defined.

The workaround is to wrap all of the implicit schemas with new mongoose.Schema().

@ksmithut ksmithut changed the title toJSON options gets passed to implicitly created schemas toJSON options gets passed to implicitly created schemas, including transform Jul 10, 2023
@vkarpov15 vkarpov15 added this to the 7.4.1 milestone Jul 12, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jul 12, 2023
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const schema = new mongoose.Schema(
  {
    items: [{ key: String }]
  },
  {
    toJSON: {
      transform (doc, obj) {
        console.log(obj)
        return obj
      }
    }
  }
)
const MyModel = mongoose.model('MyModel', schema)

async function run() {
  await mongoose.connect('mongodb://localhost:27017/testing')
  await mongoose.connection.dropDatabase();
  const doc = await MyModel.create({
    items: [{ key: 'foobar' }]
  })
  doc.toJSON()
  await mongoose.disconnect()
}

run();

@IslandRhythms IslandRhythms 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 Jul 14, 2023
vkarpov15 added a commit that referenced this issue Jul 19, 2023
…form` option to implicitly created schemas

Fix #13599
vkarpov15 added a commit that referenced this issue Jul 20, 2023
fix(schema): avoid propagating `toObject.transform` and `toJSON.transform` option to implicitly created schemas
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