@@ -12769,6 +12769,28 @@ describe('document', function() {
12769
12769
) ;
12770
12770
} ) ;
12771
12771
12772
+ it ( 'handles reusing schema with embedded discriminators defined using Schema.prototype.discriminator (gh-14162)' , async function ( ) {
12773
+ const discriminated = new Schema ( {
12774
+ type : { type : Number , required : true }
12775
+ } , { discriminatorKey : 'type' } ) ;
12776
+
12777
+ discriminated . discriminator ( 1 , new Schema ( { prop1 : String } ) ) ;
12778
+ discriminated . discriminator ( 3 , new Schema ( { prop2 : String } ) ) ;
12779
+
12780
+ const containerSchema = new Schema ( { items : [ discriminated ] } ) ;
12781
+ const containerModel = db . model ( 'Test' , containerSchema ) ;
12782
+ const containerModel2 = db . model ( 'Test1' , containerSchema ) ;
12783
+ const doc1 = new containerModel ( { items : [ { type : 1 , prop1 : 'foo' } , { type : 3 , prop2 : 'bar' } ] } ) ;
12784
+ const doc2 = new containerModel2 ( { items : [ { type : 1 , prop1 : 'baz' } , { type : 3 , prop2 : 'qux' } ] } ) ;
12785
+ await doc1 . save ( ) ;
12786
+ await doc2 . save ( ) ;
12787
+
12788
+ doc1 . items . push ( { type : 3 , prop2 : 'test1' } ) ;
12789
+ doc2 . items . push ( { type : 3 , prop2 : 'test1' } ) ;
12790
+ await doc1 . save ( ) ;
12791
+ await doc2 . save ( ) ;
12792
+ } ) ;
12793
+
12772
12794
it ( 'can use `collection` as schema name (gh-13956)' , async function ( ) {
12773
12795
const schema = new mongoose . Schema ( { name : String , collection : String } ) ;
12774
12796
const Test = db . model ( 'Test' , schema ) ;
@@ -12802,6 +12824,98 @@ describe('document', function() {
12802
12824
[ '__stateBeforeSuspension' , '__stateBeforeSuspension.jsonField' ]
12803
12825
) ;
12804
12826
} ) ;
12827
+
12828
+ it ( 'should allow null values in list in self assignment (gh-14172) (gh-13859)' , async function ( ) {
12829
+ const objSchema = new Schema ( {
12830
+ date : Date ,
12831
+ value : Number
12832
+ } ) ;
12833
+
12834
+ const testSchema = new Schema ( {
12835
+ intArray : [ Number ] ,
12836
+ strArray : [ String ] ,
12837
+ objArray : [ objSchema ]
12838
+ } ) ;
12839
+ const Test = db . model ( 'Test' , testSchema ) ;
12840
+
12841
+ const doc = new Test ( {
12842
+ intArray : [ 1 , 2 , 3 , null ] ,
12843
+ strArray : [ 'b' , null , 'c' ] ,
12844
+ objArray : [
12845
+ { date : new Date ( 1000 ) , value : 1 } ,
12846
+ null ,
12847
+ { date : new Date ( 3000 ) , value : 3 }
12848
+ ]
12849
+ } ) ;
12850
+ await doc . save ( ) ;
12851
+ doc . intArray = doc . intArray ;
12852
+ doc . strArray = doc . strArray ;
12853
+ doc . objArray = doc . objArray ; // this is the trigger for the error
12854
+ assert . ok ( doc ) ;
12855
+ await doc . save ( ) ;
12856
+ assert . ok ( doc ) ;
12857
+ } ) ;
12858
+
12859
+ it ( 'avoids overwriting dotted paths in mixed path underneath nested path (gh-14178)' , async function ( ) {
12860
+ const testSchema = new Schema ( {
12861
+ __stateBeforeSuspension : {
12862
+ field1 : String ,
12863
+ field3 : { type : Schema . Types . Mixed }
12864
+ }
12865
+ } ) ;
12866
+ const Test = db . model ( 'Test' , testSchema ) ;
12867
+ const eventObj = new Test ( {
12868
+ __stateBeforeSuspension : { field1 : 'test' }
12869
+ } ) ;
12870
+ await eventObj . save ( ) ;
12871
+ const newO = eventObj . toObject ( ) ;
12872
+ newO . __stateBeforeSuspension . field3 = { '.ippo' : 5 } ;
12873
+ eventObj . set ( newO ) ;
12874
+ await eventObj . save ( ) ;
12875
+
12876
+ assert . strictEqual ( eventObj . __stateBeforeSuspension . field3 [ '.ippo' ] , 5 ) ;
12877
+
12878
+ const fromDb = await Test . findById ( eventObj . _id ) . lean ( ) . orFail ( ) ;
12879
+ assert . strictEqual ( fromDb . __stateBeforeSuspension . field3 [ '.ippo' ] , 5 ) ;
12880
+ } ) ;
12881
+
12882
+ it ( 'handles setting nested path to null (gh-14205)' , function ( ) {
12883
+ const schema = new mongoose . Schema ( {
12884
+ nested : {
12885
+ key1 : String ,
12886
+ key2 : String
12887
+ }
12888
+ } ) ;
12889
+
12890
+ const Model = db . model ( 'Test' , schema ) ;
12891
+
12892
+ const doc = new Model ( ) ;
12893
+ doc . init ( {
12894
+ nested : { key1 : 'foo' , key2 : 'bar' }
12895
+ } ) ;
12896
+
12897
+ doc . set ( { nested : null } ) ;
12898
+ assert . strictEqual ( doc . toObject ( ) . nested , null ) ;
12899
+ } ) ;
12900
+
12901
+ it ( 'handles setting nested path to undefined (gh-14205)' , function ( ) {
12902
+ const schema = new mongoose . Schema ( {
12903
+ nested : {
12904
+ key1 : String ,
12905
+ key2 : String
12906
+ }
12907
+ } ) ;
12908
+
12909
+ const Model = db . model ( 'Test' , schema ) ;
12910
+
12911
+ const doc = new Model ( ) ;
12912
+ doc . init ( {
12913
+ nested : { key1 : 'foo' , key2 : 'bar' }
12914
+ } ) ;
12915
+
12916
+ doc . set ( { nested : void 0 } ) ;
12917
+ assert . strictEqual ( doc . toObject ( ) . nested , void 0 ) ;
12918
+ } ) ;
12805
12919
} ) ;
12806
12920
12807
12921
describe ( 'Check if instance function that is supplied in schema option is availabe' , function ( ) {
0 commit comments