Skip to content

Commit

Permalink
Merge pull request #13135 from Automattic/vkarpov15/gh-13003
Browse files Browse the repository at this point in the history
fix(document): avoid setting array default if document array projected out by sibling projection
  • Loading branch information
vkarpov15 authored Mar 6, 2023
2 parents a706bc3 + 978b49d commit b8ed1d9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/helpers/document/applyDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre
}
} else if (exclude === false && fields && !included) {
const hasSubpaths = type.$isSingleNested || type.$isMongooseDocumentArray;
if (curPath in fields || (hasSubpaths && hasIncludedChildren != null && hasIncludedChildren[curPath])) {
if (curPath in fields || (j === len - 1 && hasSubpaths && hasIncludedChildren != null && hasIncludedChildren[curPath])) {
included = true;
} else if (hasIncludedChildren != null && !hasIncludedChildren[curPath]) {
break;
Expand Down
14 changes: 12 additions & 2 deletions test/docs/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,20 @@ describe('debug: shell', function() {
});

it('should avoid sending null session option with document ops (gh-13052)', async function() {
let args = [];
const m = new mongoose.Mongoose();
m.set('debug', function() {
args.push([...arguments]);
});
await m.connect(start.uri);
const schema = new Schema({ name: String });
const Test = db.model('gh_13052', schema);
const Test = m.model('gh_13052', schema);

await Test.create({ name: 'foo' });
assert.equal(false, lastLog.includes('session'));
assert.equal(args.length, 1);
assert.equal(args[0][1], 'insertOne');
assert.ok(!('session' in args[0][3]));

await m.disconnect();
});
});
58 changes: 58 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12162,6 +12162,64 @@ describe('document', function() {
{ $pop: { arr: -1 }, $inc: { __v: 1 } }
);
});

it('avoids setting array default if document array projected out by sibling projection (gh-13003)', async function() {
const schema = new mongoose.Schema({
name: String,
arr: [String],
properties: {
foo: String,
bar: [{ baz: String, qux: Boolean }],
baz: String
}
});
const Test = db.model('Test', schema);

const doc = new Test({}, { 'properties.foo': 1 });
doc.init({ properties: { foo: 'foo' } });
assert.strictEqual(doc.properties.bar, undefined);
});

it('avoids overwriting array with sibling projection (gh-13043)', async function() {
const testSchema = new mongoose.Schema({
str: 'string',
obj: {
subObj: {
str: 'string'
},
subArr: [{
str: 'string'
}]
},
arr: [{
str: 'string'
}]
});
const Test = db.model('Test', testSchema);
// Create one test document : obj.subArr[0].str === 'subArr.test1'
await Test.create({
str: 'test1',
obj: {
subObj: {
str: 'subObj.test1'
},
subArr: [{
str: 'subArr.test1'
}]
},
arr: [{ str: 'arr.test1' }]
});

const test = await Test.findOne({ str: 'test1' }, 'str obj.subObj');

// Update one property
test.str = test.str + ' - updated';
await test.save();

const fromDb = await Test.findById(test);
assert.equal(fromDb.obj.subArr.length, 1);
assert.equal(fromDb.obj.subArr[0].str, 'subArr.test1');
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down

0 comments on commit b8ed1d9

Please sign in to comment.