@@ -8955,4 +8955,48 @@ describe('model: populate:', function() {
89558955 assert . deepEqual ( doc . toObject ( { virtuals : true } ) . things , [ ] ) ;
89568956 } ) ;
89578957 } ) ;
8958+
8959+ it ( 'succeeds with refPath if embedded discriminator has path with same name but no refPath (gh-8452)' , function ( ) {
8960+ const ImageSchema = Schema ( { imageName : String } ) ;
8961+ const Image = db . model ( 'gh8452_Image' , ImageSchema ) ;
8962+
8963+ const TextSchema = Schema ( { textName : String } ) ;
8964+ const Text = db . model ( 'gh8452_Text' , TextSchema ) ;
8965+
8966+ const opts = { _id : false } ;
8967+ const ItemSchema = Schema ( { objectType : String } , opts ) ;
8968+ const ItemSchemaA = Schema ( {
8969+ data : {
8970+ type : ObjectId ,
8971+ refPath : 'list.objectType'
8972+ } ,
8973+ objectType : String ,
8974+ } , opts ) ;
8975+ const ItemSchemaB = Schema ( {
8976+ data : { sourceId : Number } ,
8977+ objectType : String ,
8978+ } , opts ) ;
8979+
8980+ const ExampleSchema = Schema ( { test : String , list : [ ItemSchema ] } ) ;
8981+ ExampleSchema . path ( 'list' ) . discriminator ( 'gh8452_ExtendA' , ItemSchemaA ) ;
8982+ ExampleSchema . path ( 'list' ) . discriminator ( 'gh8452_ExtendB' , ItemSchemaB ) ;
8983+ const Example = db . model ( 'gh8452_Example' , ExampleSchema ) ;
8984+
8985+ return co ( function * ( ) {
8986+ const image = yield Image . create ( { imageName : 'image' } ) ;
8987+ const text = yield Text . create ( { textName : 'text' } ) ;
8988+ yield Example . create ( {
8989+ test : '02' ,
8990+ list : [
8991+ { __t : 'gh8452_ExtendA' , data : image . _id , objectType : 'gh8452_Image' } ,
8992+ { __t : 'gh8452_ExtendA' , data : text . _id , objectType : 'gh8452_Text' } ,
8993+ { __t : 'gh8452_ExtendB' , data : { sourceId : 123 } , objectType : 'ExternalSourceA' }
8994+ ]
8995+ } ) ;
8996+
8997+ const res = yield Example . findOne ( ) . populate ( 'list.data' ) . lean ( ) ;
8998+ assert . equal ( res . list [ 0 ] . data . imageName , 'image' ) ;
8999+ assert . equal ( res . list [ 1 ] . data . textName , 'text' ) ;
9000+ } ) ;
9001+ } ) ;
89589002} ) ;
0 commit comments