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

bug: field with alias: 'id' has conflict with schema automatic id setter #13650

Closed
2 tasks done
Kamikadze4GAME opened this issue Jul 24, 2023 · 0 comments · Fixed by #13654
Closed
2 tasks done

bug: field with alias: 'id' has conflict with schema automatic id setter #13650

Kamikadze4GAME opened this issue Jul 24, 2023 · 0 comments · Fixed by #13654
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Kamikadze4GAME
Copy link
Contributor

Kamikadze4GAME commented Jul 24, 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.4.0

Node.js version

16.20.0

MongoDB server version

6.0.6

Typescript version (if applicable)

No response

Description

After updating mongoose to version 7.4.0, an error appeared (everything worked correctly on version 7.3.4).

Adding to schema a field with alias: 'id' causes strange setter behaviour (see Steps to Reproduce). The bug is related to the commit 32a84b7

Steps to Reproduce

Steps to Reproduce:

const mongoose = require('mongoose');
const { Schema } = mongoose;

const removeMultipleSpaces = (val) => {
  console.log('removeMultipleSpaces', val, typeof val);
  return val.replace(/\s+/g, ' ');
};


run()
  .then(() => console.log('done'))
  .catch(error => console.error(error))
  .then(() => process.exit(0));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/gh13650');
  await mongoose.connection.dropDatabase();


  const HouseSchema = new Schema({
    _id: {
      type: String,
      required: true,
      set: removeMultipleSpaces,
      alias: 'id',
    },
  }, {
    // id: false
  });

  const House = mongoose.model('House', HouseSchema);

  const house = new House({
    id: 'H-1',
  });

  await house.save();


  await mongoose.disconnect();
}

Output:

removeMultipleSpaces H-1 string
removeMultipleSpaces { _id: 'H-1' } object          <<------------ Look here
Error: House validation failed: _id: Cast to String failed for value "{ _id: 'H-1' }" (type model) at path "_id" because of "TypeError"
    at ValidationError.inspect (mongoose_bugreport/node_modules/mongoose/lib/error/validation.js:50:26)
    at formatValue (node:internal/util/inspect:806:19)
    at inspect (node:internal/util/inspect:365:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2292:40)
    at formatWithOptions (node:internal/util/inspect:2154:10)
    at console.value (node:internal/console/constructor:348:14)
    at console.warn (node:internal/console/constructor:381:61)
    at mongoose_bugreport/mongoose.bug.0000.js:12:27
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  errors: {
    _id: CastError: Cast to String failed for value "{ _id: 'H-1' }" (type model) at path "_id" because of "TypeError"
        at model.$set (mongoose_bugreport/node_modules/mongoose/lib/document.js:1460:9)
        at model.set [as _id] (mongoose_bugreport/node_modules/mongoose/lib/helpers/document/compile.js:205:19)
        at model.idSetter (mongoose_bugreport/node_modules/mongoose/lib/helpers/schema/idGetter.js:41:12)
        at VirtualType.applySetters (mongoose_bugreport/node_modules/mongoose/lib/virtualtype.js:166:16)
        at model.$set (mongoose_bugreport/node_modules/mongoose/lib/document.js:1271:12)
        at model.$set (mongoose_bugreport/node_modules/mongoose/lib/document.js:1113:16)
        at model.Document (mongoose_bugreport/node_modules/mongoose/lib/document.js:164:12)
        at model.Model (mongoose_bugreport/node_modules/mongoose/lib/model.js:122:12)
        at new model (mongoose_bugreport/node_modules/mongoose/lib/model.js:4684:15)
        at run (mongoose_bugreport/mongoose.bug.0000.js:33:17) {
      stringValue: `"{ _id: 'H-1' }"`,
      messageFormat: undefined,
      kind: 'String',
      value: [Object],
      path: '_id',
      reason: TypeError: val.replace is not a function
          at model.removeMultipleSpaces (mongoose_bugreport/mongoose.bug.0000.js:6:14)
          at SchemaString.SchemaType._applySetters (mongoose_bugreport/node_modules/mongoose/lib/schematype.js:1189:20)
          at SchemaString.SchemaType.applySetters (mongoose_bugreport/node_modules/mongoose/lib/schematype.js:1214:16)
          at model.$set (mongoose_bugreport/node_modules/mongoose/lib/document.js:1411:22)
          at model.set [as _id] (mongoose_bugreport/node_modules/mongoose/lib/helpers/document/compile.js:205:19)
          at model.idSetter (mongoose_bugreport/node_modules/mongoose/lib/helpers/schema/idGetter.js:41:12)
          at VirtualType.applySetters (mongoose_bugreport/node_modules/mongoose/lib/virtualtype.js:166:16)
          at model.$set (mongoose_bugreport/node_modules/mongoose/lib/document.js:1271:12)
          at model.$set (mongoose_bugreport/node_modules/mongoose/lib/document.js:1113:16)
          at model.Document (mongoose_bugreport/node_modules/mongoose/lib/document.js:164:12),
      valueType: 'model'
    }
  },
  _message: 'House validation failed'
}

Expected Behavior

I understand that object validation fails because of the CastError error, and it is called because an object is passed to removeMultipleSpaces and a TypeError already occurs there.

I found out that there are several ways to solve the problem:

  1. Remove alias: 'id'. Since version 7.4.0 added an automatic setter for the id virtual field (commit: 32a84b7).
  2. Add the { id: false } option to the schema to disable automatic addition of the id virtual field with its getter/setter
  3. Add an argument check to removeMultipleSpaces that a string came in (but this is strange because I defined the setter for a string field).

Also I suggest adding a new feature to mongoose:

If there is some field with alias: 'id', then automatically set the option { id: false } for the schema

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 25, 2023
@IslandRhythms IslandRhythms self-assigned this Jul 25, 2023
vkarpov15 added a commit that referenced this issue Jul 25, 2023
@vkarpov15 vkarpov15 added this to the 7.4.2 milestone Jul 25, 2023
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.

3 participants