@@ -12961,6 +12961,100 @@ describe('document', function() {
1296112961 doc . set ( { nested : void 0 } ) ;
1296212962 assert . strictEqual ( doc . toObject ( ) . nested , void 0 ) ;
1296312963 } ) ;
12964+
12965+ it ( 'avoids depopulating populated subdocs underneath document arrays when copying to another document (gh-14418)' , async function ( ) {
12966+ const cartSchema = new mongoose . Schema ( {
12967+ products : [
12968+ {
12969+ product : {
12970+ type : mongoose . Schema . Types . ObjectId ,
12971+ ref : 'Product'
12972+ } ,
12973+ quantity : Number
12974+ }
12975+ ] ,
12976+ singleProduct : {
12977+ type : mongoose . Schema . Types . ObjectId ,
12978+ ref : 'Product'
12979+ }
12980+ } ) ;
12981+ const purchaseSchema = new mongoose . Schema ( {
12982+ products : [
12983+ {
12984+ product : {
12985+ type : mongoose . Schema . Types . ObjectId ,
12986+ ref : 'Product'
12987+ } ,
12988+ quantity : Number
12989+ }
12990+ ] ,
12991+ singleProduct : {
12992+ type : mongoose . Schema . Types . ObjectId ,
12993+ ref : 'Product'
12994+ }
12995+ } ) ;
12996+ const productSchema = new mongoose . Schema ( {
12997+ name : String
12998+ } ) ;
12999+
13000+ const Cart = db . model ( 'Cart' , cartSchema ) ;
13001+ const Purchase = db . model ( 'Purchase' , purchaseSchema ) ;
13002+ const Product = db . model ( 'Product' , productSchema ) ;
13003+
13004+ const dbProduct = await Product . create ( { name : 'Bug' } ) ;
13005+
13006+ const dbCart = await Cart . create ( {
13007+ products : [
13008+ {
13009+ product : dbProduct ,
13010+ quantity : 2
13011+ }
13012+ ] ,
13013+ singleProduct : dbProduct
13014+ } ) ;
13015+
13016+ const foundCart = await Cart . findById ( dbCart . _id ) .
13017+ populate ( 'products.product singleProduct' ) ;
13018+
13019+ const purchaseFromDbCart = new Purchase ( {
13020+ products : foundCart . products ,
13021+ singleProduct : foundCart . singleProduct
13022+ } ) ;
13023+ assert . equal ( purchaseFromDbCart . products [ 0 ] . product . name , 'Bug' ) ;
13024+ assert . equal ( purchaseFromDbCart . singleProduct . name , 'Bug' ) ;
13025+ } ) ;
13026+
13027+ it ( 'handles virtuals that are stored as objects but getter returns string with toJSON (gh-14446)' , async function ( ) {
13028+ const childSchema = new mongoose . Schema ( ) ;
13029+
13030+ childSchema . virtual ( 'name' )
13031+ . set ( function ( values ) {
13032+ for ( const [ lang , val ] of Object . entries ( values ) ) {
13033+ this . set ( `name.${ lang } ` , val ) ;
13034+ }
13035+ } )
13036+ . get ( function ( ) {
13037+ return this . $__getValue ( `name.${ this . lang } ` ) ;
13038+ } ) ;
13039+
13040+ childSchema . add ( { name : { en : { type : String } , de : { type : String } } } ) ;
13041+
13042+ const ChildModel = db . model ( 'Child' , childSchema ) ;
13043+ const ParentModel = db . model ( 'Parent' , new mongoose . Schema ( {
13044+ children : [ childSchema ]
13045+ } ) ) ;
13046+
13047+ const child = await ChildModel . create ( { name : { en : 'Stephen' , de : 'Stefan' } } ) ;
13048+ child . lang = 'en' ;
13049+ assert . equal ( child . name , 'Stephen' ) ;
13050+
13051+ const parent = await ParentModel . create ( {
13052+ children : [ { name : { en : 'Stephen' , de : 'Stefan' } } ]
13053+ } ) ;
13054+ parent . children [ 0 ] . lang = 'de' ;
13055+ const obj = parent . toJSON ( { getters : true } ) ;
13056+ assert . equal ( obj . children [ 0 ] . name , 'Stefan' ) ;
13057+ } ) ;
1296413058} ) ;
1296513059
1296613060describe ( 'Check if instance function that is supplied in schema option is availabe' , function ( ) {
0 commit comments