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

PR #12254 introduced bug: Methods defined in shared schemas get removed #12423

Closed
2 tasks done
maximilianschmid opened this issue Sep 12, 2022 · 5 comments · Fixed by #12453
Closed
2 tasks done

PR #12254 introduced bug: Methods defined in shared schemas get removed #12423

maximilianschmid opened this issue Sep 12, 2022 · 5 comments · Fixed by #12453
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@maximilianschmid
Copy link

maximilianschmid commented Sep 12, 2022

Prerequisites

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

Mongoose version

6.5.5

Node.js version

16.6

MongoDB server version

6.0.1

Description

PR #12254 introduces a bug
where instance methods defined in shared schema get deleted.

Problem located in line 38 in file
mongoose/lib/helpers/model/applyMethods.js

Commit
9fbf92c#diff-9de3851c9d06234f6bef122196730d35ce2dc68fc2b609824b1676194608e4fb

Steps to Reproduce

  1. Install mongoose 6.5.5
  2. Run this bug demo script:
const mongoose = require('mongoose')
mongoose.set('debug', true)

const sharedSubSchema = new mongoose.Schema({
  name: {
    type: String
  }
})

sharedSubSchema.methods.sharedSubSchemaMethod = function () {
  console.log('sharedSubSchemaMethod called')
}

const mainDocumentSchema = new mongoose.Schema({
  subdocuments: {
    type: [sharedSubSchema],
    required: true
  }
})
const MainDocument = mongoose.model('MainDocument', mainDocumentSchema)

const secondaryDocumentSchema = new mongoose.Schema({
  subdocuments: {
    type: [sharedSubSchema],
    required: true
  }
})
const SecondaryDocument = mongoose.model('SecondaryDocument', secondaryDocumentSchema)

const mainDoc = new MainDocument({
  subdocuments: [
    {
      name: 'one'
    }
  ]
})

const secondaryDoc = new SecondaryDocument({
  subdocuments: [
    {
      name: 'secondary'
    }
  ]
})

console.log(mainDoc.subdocuments[0].sharedSubSchemaMethod)
console.log(secondaryDoc.subdocuments[0].sharedSubSchemaMethod)

Expected Behavior

Sub-Document method should be defined on both models, not only one.

Buggy output mongoosejs 6.5.5
[Function (anonymous)]
undefined

Correct output mongoosejs 6.5.4
[Function (anonymous)]
[Function (anonymous)]

@maximilianschmid maximilianschmid changed the title PR #12254 introduced major bug: Methods defined in shared schemas get removed PR #12254 introduced bug: Methods defined in shared schemas get removed Sep 12, 2022
@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Sep 12, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose')
mongoose.set('debug', true)

const sharedSubSchema = new mongoose.Schema({
  name: {
    type: String
  }
})

sharedSubSchema.methods.sharedSubSchemaMethod = function () {
  console.log('sharedSubSchemaMethod called')
}

const mainDocumentSchema = new mongoose.Schema({
  subdocuments: {
    type: [sharedSubSchema],
    required: true
  }
})
const MainDocument = mongoose.model('MainDocument', mainDocumentSchema)

const secondaryDocumentSchema = new mongoose.Schema({
  subdocuments: {
    type: [sharedSubSchema],
    required: true
  }
})
const SecondaryDocument = mongoose.model('SecondaryDocument', secondaryDocumentSchema)


async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();
  
  const mainDoc = await MainDocument.create({
    subdocuments: [
      {
        name: 'one'
      }
    ]
  })
  
  const secondaryDoc = await SecondaryDocument.create({
    subdocuments: [
      {
        name: 'secondary'
      }
    ]
  })
  
  console.log(mainDoc.subdocuments[0].sharedSubSchemaMethod)
  console.log(secondaryDoc.subdocuments[0].sharedSubSchemaMethod)
}

run();

@maximilianschmid
Copy link
Author

👍

@maximilianschmid
Copy link
Author

Just to clarify: version 6.6.0 also has this bug.

@backflip
Copy link

Just stumbled upon the same problem. For reference: The corresponding PR is #12391

@maximilianschmid
Copy link
Author

Thx!

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
Development

Successfully merging a pull request may close this issue.

4 participants