@@ -12765,6 +12765,28 @@ describe('document', function() {
12765
12765
) ;
12766
12766
} ) ;
12767
12767
12768
+ it ( 'handles reusing schema with embedded discriminators defined using Schema.prototype.discriminator (gh-14162)' , async function ( ) {
12769
+ const discriminated = new Schema ( {
12770
+ type : { type : Number , required : true }
12771
+ } , { discriminatorKey : 'type' } ) ;
12772
+
12773
+ discriminated . discriminator ( 1 , new Schema ( { prop1 : String } ) ) ;
12774
+ discriminated . discriminator ( 3 , new Schema ( { prop2 : String } ) ) ;
12775
+
12776
+ const containerSchema = new Schema ( { items : [ discriminated ] } ) ;
12777
+ const containerModel = db . model ( 'Test' , containerSchema ) ;
12778
+ const containerModel2 = db . model ( 'Test1' , containerSchema ) ;
12779
+ const doc1 = new containerModel ( { items : [ { type : 1 , prop1 : 'foo' } , { type : 3 , prop2 : 'bar' } ] } ) ;
12780
+ const doc2 = new containerModel2 ( { items : [ { type : 1 , prop1 : 'baz' } , { type : 3 , prop2 : 'qux' } ] } ) ;
12781
+ await doc1 . save ( ) ;
12782
+ await doc2 . save ( ) ;
12783
+
12784
+ doc1 . items . push ( { type : 3 , prop2 : 'test1' } ) ;
12785
+ doc2 . items . push ( { type : 3 , prop2 : 'test1' } ) ;
12786
+ await doc1 . save ( ) ;
12787
+ await doc2 . save ( ) ;
12788
+ } ) ;
12789
+
12768
12790
it ( 'can use `collection` as schema name (gh-13956)' , async function ( ) {
12769
12791
const schema = new mongoose . Schema ( { name : String , collection : String } ) ;
12770
12792
const Test = db . model ( 'Test' , schema ) ;
0 commit comments