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

fix(array): track correct changes when setting nested array of primitives #13422

Merged
merged 5 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/schema/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,11 @@ SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
options = options || emptyOpts;

let rawValue = utils.isMongooseArray(value) ? value.__array : value;
value = MongooseArray(rawValue, options.path || this._arrayPath || this.path, doc, this);
let path = options.path || this.path;
if (options.arrayPathIndex != null) {
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
path += '.' + options.arrayPathIndex;
}
value = MongooseArray(rawValue, path, doc, this);
rawValue = value.__array;

if (init && doc != null && doc.$__ != null && doc.$populated(this.path)) {
Expand Down
2 changes: 1 addition & 1 deletion test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2562,7 +2562,7 @@ describe('schema', function() {
});

const casted = schema.path('ids').cast([[]]);
assert.equal(casted[0].$path(), 'ids.$');
assert.equal(casted[0].$path(), 'ids.0');
});

describe('cast option (gh-8407)', function() {
Expand Down
16 changes: 16 additions & 0 deletions test/types.array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1899,4 +1899,20 @@ describe('types array', function() {

m.Schema.Types.Array.options.castNonArrays = true;
});

it('supports setting nested arrays directly (gh-13372)', function() {
const Test = db.model('Test', new Schema({ intArr: [[Number]] }));

const intArr = [[1, 2], [3, 4]];
const doc = Test.hydrate({ intArr });
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved

doc.intArr[0][0] = 2;
doc.intArr[1][1] = 5;
assert.deepStrictEqual(doc.getChanges(), {
$set: {
'intArr.0.0': 2,
'intArr.1.1': 5
}
});
});
});