Skip to content

Commit 6336ed6

Browse files
committed
one failing test
1 parent f58c6c2 commit 6336ed6

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

lib/schema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,9 @@ Schema.prototype._clone = function _clone(Constructor) {
412412
s.s.hooks = this.s.hooks.clone();
413413

414414
s.tree = clone(this.tree);
415+
// recursion is triggered here
415416
s.paths = Object.fromEntries(
416-
Object.entries(this.paths).map(([key, value]) => ([key, value.clone()]))
417+
Object.entries(this.paths).map(([key, value]) => ([key, value]))
417418
);
418419
s.nested = clone(this.nested);
419420
s.subpaths = clone(this.subpaths);

lib/schema/SubdocumentPath.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ function SubdocumentPath(schema, path, options) {
5555
this.$isSingleNested = true;
5656
this.base = schema.base;
5757
SchemaType.call(this, path, options, 'Embedded');
58-
5958
if (schema._applyDiscriminators != null && !options?._skipApplyDiscriminators) {
6059
for (const disc of schema._applyDiscriminators.keys()) {
6160
this.discriminator(disc, schema._applyDiscriminators.get(disc));

test/schema.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3188,4 +3188,18 @@ describe('schema', function() {
31883188
const doc = new TestModel({ type: 1, self: { type: 1 } });
31893189
assert.strictEqual(doc.self.type, 1);
31903190
});
3191+
it('handles recursive definitions of arrays in discriminators (gh-14055)', function() {
3192+
const base = new Schema({
3193+
type: { type: Number, required: true }
3194+
}, { discriminatorKey: 'type' });
3195+
3196+
const recursive = new Schema({
3197+
self: { type: [base], required: true }
3198+
});
3199+
3200+
base.discriminator(1, recursive);
3201+
const baseModel = db.model('gh14055', base);
3202+
const doc = new baseModel({ type: 1, self: [{ type: 1 }] });
3203+
assert.equal(doc.self[0].type, 1);
3204+
});
31913205
});

0 commit comments

Comments
 (0)