Skip to content

Commit 49b406c

Browse files
authored
Merge pull request #14068 from Automattic/IslandRhythms/gh-14055
Island rhythms/gh 14055
2 parents f58c6c2 + ab360ff commit 49b406c

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

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));

lib/schema/documentarray.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function DocumentArrayPath(key, schema, options, schemaOptions) {
8989
this.$embeddedSchemaType.caster = this.Constructor;
9090
this.$embeddedSchemaType.schema = this.schema;
9191

92-
if (schema._applyDiscriminators != null) {
92+
if (schema._applyDiscriminators != null && !options?._skipApplyDiscriminators) {
9393
for (const disc of schema._applyDiscriminators.keys()) {
9494
this.discriminator(disc, schema._applyDiscriminators.get(disc));
9595
}
@@ -528,7 +528,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
528528

529529
DocumentArrayPath.prototype.clone = function() {
530530
const options = Object.assign({}, this.options);
531-
const schematype = new this.constructor(this.path, this.schema, options, this.schemaOptions);
531+
const schematype = new this.constructor(this.path, this.schema, { ...options, _skipApplyDiscriminators: true }, this.schemaOptions);
532532
schematype.validators = this.validators.slice();
533533
if (this.requiredValidator !== undefined) {
534534
schematype.requiredValidator = this.requiredValidator;

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)