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

Wrong field processing order with discriminators #13794

Closed
2 tasks done
piyushk96 opened this issue Aug 29, 2023 · 0 comments · Fixed by #13846
Closed
2 tasks done

Wrong field processing order with discriminators #13794

piyushk96 opened this issue Aug 29, 2023 · 0 comments · Fixed by #13846
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@piyushk96
Copy link

piyushk96 commented Aug 29, 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.5

Node.js version

16

MongoDB server version

N/A

Typescript version (if applicable)

N/A

Description

When using discriminator sub schema fields are processed before the base schema fields but according to mongoose documentation the fields should be processed in order of their definition in schema.

In my use case, I need to set and validate values of the sub schema fields according to a field of base schema. But since sub schema fields are processed first the base schema fields are coming to be undefined in sub schema field's setters and validators.

Steps to Reproduce

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

mongoose.connect('mongodb://localhost:27017/testdb');

const E1 = new Schema({
  f1: {
    type: Number,
    set() {
      console.log('Set 1');
      return 1;
    },
    validate: [
      {
        validator: (v) => {
          console.log('Validator 1');
          return true;
        }
      }
    ]
  },

});
const E1Model = mongoose.model('E1', E1);

const E2 = new Schema({
  f2: {
    type: Number,
    set() {
      console.log('Set 2');
      return 2;
    },
    validate: [
      {
        validator: (v) => {
          console.log('Validator 2');
          return true;
        }
      }
    ]
  },
});
const E2Model = E1Model.discriminator('E2', E2, { discriminatorKey: 'kind' })

async function main() {
  let item1 = new E2Model({ f1: 1, f2: 2 });
  item1 = await item1.save()
}

main();

Expected Behavior

The setters & validators of base class fields should be processed before sub class fields

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Aug 29, 2023
@vkarpov15 vkarpov15 added this to the 7.5.1 milestone Sep 1, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.5.1, 7.5.2 Sep 8, 2023
vkarpov15 added a commit that referenced this issue Sep 9, 2023
…schema paths when running setters, validators, etc.

Fix #13794
vkarpov15 added a commit that referenced this issue Sep 15, 2023
fix(discriminator): make base schema paths come before discriminator schema paths when running setters, validators, etc.
@vkarpov15 vkarpov15 modified the milestones: 7.5.2, 8.0 Sep 15, 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
3 participants