@@ -3889,4 +3889,34 @@ describe('schema', function() {
3889
3889
assert . throws ( ( ) => schema . toJSONSchema ( ) , / u n s u p p o r t e d S c h e m a T y p e t o J S O N S c h e m a : M i x e d / ) ;
3890
3890
} ) ;
3891
3891
} ) ;
3892
+
3893
+ it ( 'path() clears existing child schemas (gh-15253)' , async function ( ) {
3894
+ const RecursiveSchema = new mongoose . Schema ( {
3895
+ data : String
3896
+ } ) ;
3897
+
3898
+ const s = [ RecursiveSchema ] ;
3899
+ RecursiveSchema . path ( 'nested' , s ) ;
3900
+ assert . strictEqual ( RecursiveSchema . childSchemas . length , 1 ) ;
3901
+ RecursiveSchema . path ( 'nested' , s ) ;
3902
+ assert . strictEqual ( RecursiveSchema . childSchemas . length , 1 ) ;
3903
+ RecursiveSchema . path ( 'nested' , s ) ;
3904
+ assert . strictEqual ( RecursiveSchema . childSchemas . length , 1 ) ;
3905
+ RecursiveSchema . path ( 'nested' , s ) ;
3906
+ assert . strictEqual ( RecursiveSchema . childSchemas . length , 1 ) ;
3907
+
3908
+ const generateRecursiveDocument = ( depth , curr = 0 ) => {
3909
+ return {
3910
+ name : `Document of depth ${ curr } ` ,
3911
+ nested : depth > 0 ? new Array ( 3 ) . fill ( ) . map ( ( ) => generateRecursiveDocument ( depth - 1 , curr + 1 ) ) : [ ] ,
3912
+ data : Math . random ( )
3913
+ } ;
3914
+ } ;
3915
+
3916
+ const TestModel = db . model ( 'Test' , RecursiveSchema ) ;
3917
+ const data = generateRecursiveDocument ( 6 ) ;
3918
+ const doc = new TestModel ( data ) ;
3919
+ await doc . save ( ) ;
3920
+
3921
+ } ) ;
3892
3922
} ) ;
0 commit comments