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

The Model constructor mutates its inputs #10766

Closed
nomcopter opened this issue Sep 23, 2021 · 2 comments
Closed

The Model constructor mutates its inputs #10766

nomcopter opened this issue Sep 23, 2021 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@nomcopter
Copy link

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

What is the current behavior?
Instantiating models mutates the input arguments. We ran into it with object ID arrays but there may be more cases.

The code below prints: Assertion failed: after

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

const mongoose = require('mongoose');

const MutatingSchema = new mongoose.Schema({
  objectIdArray: [
    {
      type: mongoose.Schema.Types.ObjectId,
    },
  ],
});
const MutatingModel = mongoose.model('MutatingModel', MutatingSchema);
const id = '614bf569793eb0b25e631952';
const objectIdArray = [id];
console.assert(id === objectIdArray[0], 'before');
new MutatingModel({ objectIdArray });
console.assert(id === objectIdArray[0], 'after');

Stack trace of where this issue appears to be happening:

    at ObjectId.SchemaType.applySetters (/.../mongoose/lib/schematype.js:1122:12)
    at SchemaArray.cast (/.../mongoose/lib/schema/array.js:385:32)
    at SchemaArray.SchemaType.applySetters (/.../mongoose/lib/schematype.js:1122:12)
    at model.$set (/.../mongoose/lib/document.js:1386:20)
    at model.$set (/.../mongoose/lib/document.js:1128:16)
    at model.Document (/.../mongoose/lib/document.js:148:12)
    at model.Model (/.../mongoose/lib/model.js:106:12)

What is the expected behavior?
Mongoose to not mutate inputs to the Model constructor.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: v14.15.1
Mongoose: v6.0.6
MongoDB: v4.1.1

@mewelling
Copy link

For what it is worth, running these same reproduction steps on mongoose v5.13.9 does not seem to produce the same issue

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Sep 23, 2021
@IslandRhythms
Copy link
Collaborator

objectIdArray goes from holding a string id to holding an object id and this did not happen in mongoose 5.x

@nomcopter nomcopter changed the title The Model constructor mutates its imports The Model constructor mutates its inputs Sep 23, 2021
@vkarpov15 vkarpov15 added this to the 6.0.9 milestone Sep 25, 2021
vkarpov15 added a commit that referenced this issue Oct 2, 2021
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

4 participants