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

StrictModeError: Throw when setting discriminator field #12513

Closed
2 tasks done
abarriel opened this issue Oct 3, 2022 · 1 comment · Fixed by #12578
Closed
2 tasks done

StrictModeError: Throw when setting discriminator field #12513

abarriel opened this issue Oct 3, 2022 · 1 comment · Fixed by #12578
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@abarriel
Copy link
Contributor

abarriel commented Oct 3, 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.6.3

Node.js version

16

MongoDB server version

4.9.1

Description

Hello, I have a throw error on kind, this is a working exemple, event when I set overwriteDiscriminatorKey to true

StrictModeError: Field kind is not in schema and strict mode is set to throw.

I think I found the issue castUpdate!L47 where overwriteDiscriminatorKey is check only after strict throw.

it only happens when you pass the updateObj directly as a second params, otherwise when we pass with $set: updateData it go through castUpdate.js!L47

if (schema.options.strict === 'throw' && obj.hasOwnProperty(schema.options.discriminatorKey)) {
    throw new StrictModeError(schema.options.discriminatorKey);
  } else if (!options.overwriteDiscriminatorKey) {
    delete obj[schema.options.discriminatorKey];
  }

Steps to Reproduce

const mongoose = require("mongoose");

mongoose.set("strict", "throw");

const options = { discriminatorKey: "kind" };
const EventSchema = new mongoose.Schema({ name: String }, options);
const Event = mongoose.model("Event", EventSchema);

async function main() {
  await mongoose.connect("mongodb://localhost:27017");

  const eventDoc = await Event.findByIdAndUpdate(
    { _id: mongoose.Types.ObjectId() },
    {
        name: "name",
        kind: "kind",
    },
    {
      overwriteDiscriminatorKey: true,
    },
  );

  console.log(eventDoc);
}

main();

Expected Behavior

No response

@abarriel
Copy link
Contributor Author

abarriel commented Oct 4, 2022

I came up with this kind of patch, where I allow developers to fetch and udpate full document and skip the strictCheck when the value of discriminator key is the same as the current model

--- a/lib/helpers/query/castUpdate.js
+++ b/lib/helpers/query/castUpdate.js
@@ -43,7 +43,7 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
     return obj;
   }
 
-  if (schema.options.strict === 'throw' && obj.hasOwnProperty(schema.options.discriminatorKey)) {
+  if (schema.options.strict === 'throw' && obj.hasOwnProperty(schema.options.discriminatorKey) && obj[schema.options.discriminatorKey] !== schema.discriminatorMapping.value) {
     throw new StrictModeError(schema.options.discriminatorKey);
   } else if (!options.overwriteDiscriminatorKey) {
     delete obj[schema.options.discriminatorKey];
@@ -196,7 +196,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
       }
     }
 
-    if (schema.discriminatorMapping != null && key === schema.options.discriminatorKey && !options.overwriteDiscriminatorKey) {
+    if (schema.discriminatorMapping != null && key === schema.options.discriminatorKey && !options.overwriteDiscriminatorKey && schema.discriminatorMapping.value !== obj[key]) {
       if (strictMode === 'throw') {
         const err = new Error('Can\'t modify discriminator key "' + key + '" on discriminator model');
         aggregatedError = _appendError(err, context, key, aggregatedError);

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Oct 10, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.6.7, 6.6.6 Oct 19, 2022
vkarpov15 added a commit that referenced this issue Oct 21, 2022
vkarpov15 added a commit that referenced this issue Oct 21, 2022
vkarpov15 added a commit that referenced this issue Oct 21, 2022
fix(query): allow overwriting discriminator key with `overwriteDiscriminatorKey` if `strict: 'throw'`
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
3 participants