Skip to content

Commit

Permalink
fix(document): avoid setting array default if document array projecte…
Browse files Browse the repository at this point in the history
…d out by sibling projection

Fix #13003
  • Loading branch information
vkarpov15 committed Mar 6, 2023
1 parent a9a838c commit da04458
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 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
7 changes: 6 additions & 1 deletion test/docs/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,15 @@ describe('debug: shell', function() {
});

it('should avoid sending null session option with document ops (gh-13052)', async function() {
const m = new mongoose.Mongoose();
m.set('debug', true);
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'));

await m.disconnect();
});
});
17 changes: 17 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12162,6 +12162,23 @@ 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);
});
});

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

0 comments on commit da04458

Please sign in to comment.