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

parent() and ownerDocument() do not work when subdoc array is initialized using default schema path option #8509

Closed
asabhaney opened this issue Jan 15, 2020 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@asabhaney
Copy link

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

Bug (breaking change)

What is the current behavior?

When using the default schema path option to create an array with a single subdoc, using parent() and ownerDocument() in the child schema's middleware seems to behave incorrectly. Encountered this issue when upgrading from mongoose 5.2.18 (where it was working as expected) to 5.8.7. I believe the changes that caused this change in behaviour happened in 5.3.x and 5.4.x.

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

const assert = require('chai').assert
const mongoose = require('mongoose')
const Schema = mongoose.Schema

it('parent() and ownerDocument() should work when subdoc array is initialized using `default` schema path option', async () => {
   // Setup our schemas + model

   const locationSchema = new Schema({
      name: String,
      city: String
   })

   // Middleware to set a default location name derived from the parent organization doc
   locationSchema.pre('validate', function (next) {
      const location = this
      console.log('Parent Doc', location.parent())
      console.log('Owner Doc', location.ownerDocument())
      if (location.isNew && !location.get('name') && location.ownerDocument().get('name')) {
         location.set('name', `${location.ownerDocument().get('name')} Office`)
      }

      next()
   })

   // Here is a schema that exemplifies the issue
   const buggyOrganizationSchema = new Schema({
      name: String,
      // Having a default doc this way causes issues
      locations: { type: [locationSchema], default: [{}] }
   })

   // Here is a similar schema, but where a default location is added using middleware
   const workingOrganizationSchema  = new Schema({
      name: String,
      locations: [locationSchema]
   })

   workingOrganizationSchema.pre('validate', function (next) {
      if (this.locations.length === 0) {
         this.locations.push({})
      }

      next()
   })

   // Models
   const WorkingOrganization = mongoose.model('working-org', workingOrganizationSchema)
   const BuggyOrganization = mongoose.model('buggy-org', buggyOrganizationSchema)

   // Create new Org doc using the working schema
   console.log('Test with default location being set via middleware')
   const abcCorp = new WorkingOrganization()
   abcCorp.set('name', 'ABC Corp')
   await abcCorp.save()
   assert(abcCorp.locations[0].name === 'ABC Corp Office')

   // Create new Org doc using the buggy schema - this will fail
   console.log('\nTest with default location being set via default schema option')
   const defCorp = new BuggyOrganization()
   defCorp.set('name', 'DEF Corp')
   await defCorp.save()
   assert(defCorp.locations[0].name === 'DEF Corp Office')
})

The console output is:

Test with default location being set via middleware
Parent Doc { _id: 5e1f89b40d3bb32dfe699b03,
  locations: [ { _id: 5e1f89b40d3bb32dfe699b04 } ],
  name: 'ABC Corp' }
Owner Doc { _id: 5e1f89b40d3bb32dfe699b03,
  locations: [ { _id: 5e1f89b40d3bb32dfe699b04 } ],
  name: 'ABC Corp' }

Test with default location being set via default schema option
Parent Doc null
Owner Doc { _id: 5e1f89b40d3bb32dfe699b06 }

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

node 8.15.0
mongoose 5.8.7 (was working as expected in 5.2.18)
MongoDB 3.6.10

@vkarpov15 vkarpov15 added this to the 5.8.10 milestone Jan 17, 2020
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue 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 Jan 17, 2020
vkarpov15 added a commit that referenced this issue Jan 21, 2020
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

No branches or pull requests

2 participants