Skip to content

[Bug] [[[Number]]] does not want to save my [[]] array correctly #9215

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

Closed
cyrilgandon opened this issue Jul 8, 2020 · 0 comments · Fixed by #9217
Closed

[Bug] [[[Number]]] does not want to save my [[]] array correctly #9215

cyrilgandon opened this issue Jul 8, 2020 · 0 comments · Fixed by #9217
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@cyrilgandon
Copy link
Contributor

cyrilgandon commented Jul 8, 2020

I have a triple nested array defined in mongoose. When I try to initialize it with a 2 level deep empty array, it adds a third level. See the following code:

import mongoose, { Schema } from 'mongoose';

const foos = mongoose.model(`FooModel`, new Schema({ numbers: [[[Number]]] }));
const oneFoo = await foos.create({ numbers: [[]] });
console.log(oneFoo.numbers);

Expected: [[]], Actual: [[[]]]

Wrap up

  • with [[[Number]]]
    • [] gives [] ✅
    • [[]] gives [[[]]] ❌
  • with [[Number]]
    • [] gives [] ✅

Versions:

"mongodb": "3.5.4",
"mongoose": "5.9.22",

The actual code only check for depth of the current value, and if it matches the depth of the declared Schema. The problem I see is that it doesn't check if values are actually array before wrapping everything into an array.

      if (valueDepth.min === valueDepth.max && valueDepth.max < depth) {
        for (let i = valueDepth.max; i < depth; ++i) {
          value = [value];
        }
      }

The way it is, it looks like it should loop over the entire array to find if depth is not sufficient AND if items are not arrays.

Reproductible unit test:

  it('doesnt wrap empty nested array with insufficient depth', function() {
    const weekSchema = mongoose.Schema({
      days: {
        type: [[[Number]]],
        required: true
      }
    });

    const Week = db.model('Test', weekSchema);
    const emptyWeek = new Week();

    emptyWeek.days = [[], [], [], [], [], [], []];
    const obj = emptyWeek.toObject();
    assert.deepEqual(obj.days, [[], [], [], [], [], [], []]);
  });

Maybe @vkarpov15 can have an idea on how to fix it, you already take care of a lot of bug on this type of issue (2b9d3b1)

@cyrilgandon cyrilgandon changed the title [Bug] Triple nested array creation failure [Bug] [[[Number]]] does not want to save my [[]] array correctly Jul 8, 2020
@vkarpov15 vkarpov15 added this to the 5.9.23 milestone Jul 8, 2020
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 8, 2020
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.

2 participants