Mongoose: 5.5.x causing CastError(s) in subdocs

Created on 11 Apr 2019  路  7Comments  路  Source: Automattic/mongoose

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

What is the current behavior?
We are currently using 5.4.19 because we were affected by this bug https://github.com/Automattic/mongoose/issues/7681 and https://github.com/Automattic/mongoose/issues/7660 so I'm not sure if it got introduced in 5.5.1 or in between the two versions but since upgrading to 5.5.1 we're seeing several test failures regarding casting errors inside embedded docs

User validation failed: purchased.plan.consecutive.offset: Cast to Number failed for value "NaN" at path "consecutive.offset", purchased.plan.consecutive.gemCapExtra: Cast to Number failed for value "NaN" at path "consecutive.gemCapExtra", ... (other fields here...)

In our schema Purchased is a subdoc of User

and

CastError: Cast to embedded failed for value "{ completed: false,\n  text: \'Checklist Item 1\',\n  id: \'274db346-3baa-46c6-bff3-ea5cff557301\' }" at path "undefined"

In this case it's also happening inside a subdoc (although an array of docs not a single one like purchased)

If the current behavior is a bug, please provide the steps to reproduce.
I've not been able to create a repro script yet but will look into it

What is the expected behavior?
No errors

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

confirmed-bug

All 7 comments

Confirmed in 5.5.0 as well, not a full repro script but looks like it's being caused by stuff like this

          task.checklist.push({
            text: 'Checklist Item 1',
            completed: false,
          });

where task.checklist is a doc of this schema https://github.com/HabitRPG/habitica/blob/develop/website/server/models/task.js#L266

The error thrown is

CastError: Cast to embedded failed for value "{ completed: false,\n  text: \'Checklist Item 1\',\n  id: \'6194d00c-0635-4ba6-86e1-49cd6a692deb\' }" at path "undefined"

and this is the stack trace

      at new CastError (node_modules/mongoose/lib/error/cast.js:29:11)
      at DocumentArray.cast (node_modules/mongoose/lib/schema/documentarray.js:409:19)
      at DocumentArray.SchemaType.applySetters (node_modules/mongoose/lib/schematype.js:864:12)
      at CoreMongooseArray.push (node_modules/mongoose/lib/types/array.js:325:27)
      ....
      trace related to our own tests from here on

Looking at the commits between 5.4.23 and 5.5.0 it looks like it is caused by https://github.com/Automattic/mongoose/commit/d4a9f9e23f9ba48cbb45585174325534af5b5b0b which is a bit weird

In particular removing this line https://github.com/Automattic/mongoose/commit/d4a9f9e23f9ba48cbb45585174325534af5b5b0b#diff-259dec4414a400a6e895f16ff1d0ca3bR852 fixes all my tests

From further investigation looks like we're experiencing two different types of bugs, this one seems related to array of subdocs when definined inside a discriminator. For the rest I'll open a new issue

Managed to make a repro script:

let IssueSchema = new Schema({
  text: String,
  type: String,
}, {
  minimize: false, // So empty objects are returned
  strict: true,
  typeKey: '$type', // So that we can use fields named `type`
  discriminatorKey: 'type',
});

IssueSchema.plugin(function (schema, options = {}) {
  if (options._id !== false) {
    schema.add({
      _id: {
        $type: String,
        default: () => String(Date.now()),
      },
    });
  }
});

const IssueModel = mongoose.model('ReproduceIssue', IssueSchema);

let SubIssueSchema = new Schema({
  checklist: [{
    completed: {$type: Boolean, default: false},
  }],
}, subDiscriminatorOptions);
IssueModel.discriminator('testsub', SubIssueSchema);

setTimeout(() => {
  const doc = new SubIssueModel({text: 'text', type: 'testsub'});
  doc.checklist.push({completed: true});
  console.log('Done');
}, 1000);

This code throws on 5.5.1 but runs fine on 5.4.23

This one proved to be pretty baffling because your script demonstrated the issue, but I couldn't repro it in our test environment. I finally got it to work with validateSync(). The fix will be in 5.5.3 :+1:

thanks @vkarpov15 ! I've confirmed that our tests are fixed with this commit

Was this page helpful?
0 / 5 - 0 ratings