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

fixes #7098 don't skip casting the update for array of array values #7207

Merged
merged 2 commits into from
Nov 2, 2018
Merged

Conversation

lineus
Copy link
Collaborator

@lineus lineus commented Nov 1, 2018

re: #7098, when updating a value in an array of arrays, we shouldn't skip query casting on the value. This change uses the schematype.$parentSchema value to detect if the current array is a top level array, only skipping casting on those that are.

I added a failing test, made it pass, all existing tests pass:

mongoose>: npm test -- -g 'gh-7098'

> mongoose@5.3.9-pre test /Users/lineus/dev/node/src/mongoose
> mocha --exit test/*.test.js test/**/*.test.js "-g" "gh-7098"


 You're not testing shards!
 Please set the MONGOOSE_SHARD_TEST_URI env variable.
 e.g: `mongodb://localhost:27017/database
 Sharding must already be enabled on your database


  !

  0 passing (381ms)
  1 failing

  1) model: update:
       bug fixes
         doesn't skip casting the query on nested arrays (gh-7098):

      AssertionError [ERR_ASSERTION]: Input A expected to strictly deep-equal input B:
+ expected - actual

- []
+ [
+   [
+     0,
+     1
+   ],
+   [
+     200,
+     3
+   ],
+   [
+     4,
+     5
+   ]
+ ]
      + expected - actual

      -[]
      +[
      +  [
      +    0
      +    1
      +  ]
      +  [
      +    200
      +    3
      +  ]
      +  [
      +    4
      +    5
      +  ]
      +]

      at /Users/lineus/dev/node/src/mongoose/test/model.update.test.js:2576:16
      at Generator.next (<anonymous>)
      at onFulfilled (node_modules/co/index.js:65:19)
      at process._tickCallback (internal/process/next_tick.js:68:7)



npm ERR! Test failed.  See above for more details.
mongoose>:
mongoose>: npm test -- -g 'gh-7098'

> mongoose@5.3.9-pre test /Users/lineus/dev/node/src/mongoose
> mocha --exit test/*.test.js test/**/*.test.js "-g" "gh-7098"


 You're not testing shards!
 Please set the MONGOOSE_SHARD_TEST_URI env variable.
 e.g: `mongodb://localhost:27017/database
 Sharding must already be enabled on your database


  ․

  1 passing (394ms)

mongoose>:
mongoose>: npm test -- --no-warnings

> mongoose@5.3.9-pre test /Users/lineus/dev/node/src/mongoose
> mocha --exit test/*.test.js test/**/*.test.js "--no-warnings"


 You're not testing shards!
 Please set the MONGOOSE_SHARD_TEST_URI env variable.
 e.g: `mongodb://localhost:27017/database
 Sharding must already be enabled on your database


  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․,,․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․,,,․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․,,,․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․,․․,․․,․,,․․,․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․,,․․․․․․․․․․․․․․,,,,․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․,․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․,,․․․․․․․․․․․․․․․․․․․․․․․․․,,․․․․․․․․․․․․․․․․․․․․
  ․․․,․․․․․․․․,․,․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․,,,,,,,,
  ,․․․․․․․․․․․․․․․․․․․․․․

  2103 passing (53s)
  37 pending

mongoose>:

the following is an expanded version of the repro code from #7098, all assertions pass with this change:

7098.js

#!/usr/bin/env node
'use strict';

const assert = require('assert');
const mongoose = require('mongoose');
const { Schema, connection} = mongoose;
const DB = '7098';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = { family: 4, useNewUrlParser: true };

const nestedSchema = new Schema({
  xyz: [[Number]]
});
const schema = new Schema({
  xyz: [[{ type: Number }]],
  nested: nestedSchema
});

const Test = mongoose.model('test', schema);

const test = new Test({
  xyz: [
    [ 0, 1 ],
    [ 2, 3 ],
    [ 4, 5 ]
  ],
  nested: {
    xyz: [
      [0, 1],
      [2, 3],
      [4, 5]
    ],
  }
});

const bulkObject = [{
  updateOne: {
    filter: { _id: test._id },
    upsert: false,
    update: { $set: { 'xyz.1.0': '200', 'nested.xyz.1.0': '200' } }
  }
}];


async function run() {
  // assert.strictEqual(mongoose.version, '5.3.8') // the assertions below fail on 5.3.8
  assert.strictEqual(mongoose.version, '5.3.9-pre');
  await mongoose.connect(URI, OPTS);
  await connection.dropDatabase();
  const inserted = await Test.create(test);
  assert.deepStrictEqual(inserted.toObject().xyz, [[0, 1], [2, 3], [4, 5]]);
  assert.deepStrictEqual(inserted.toObject().nested.xyz, [[0, 1], [2, 3], [4, 5]]);
  await Test.bulkWrite(bulkObject);
  const updated = await Test.collection.findOne({});
  assert.deepStrictEqual(updated.xyz, [[0, 1], [200, 3], [4, 5]]);
  assert.deepStrictEqual(updated.nested.xyz, [[0, 1], [200, 3], [4, 5]]);
  console.log('All Assertions Pass.');
  await connection.close();
}

run().catch(error);

function error(e) {
  console.error(e);
  return connection.close();
}

Output:

issues: ./7098.js
All Assertions Pass.
issues:

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks!

@vkarpov15 vkarpov15 merged commit c1606b6 into Automattic:master Nov 2, 2018
@vkarpov15 vkarpov15 added this to the 5.3.9 milestone Nov 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants