@@ -542,15 +542,17 @@ warnings.increment = '`increment` should not be used as a schema path name ' +
542
542
*/
543
543
544
544
Schema . prototype . path = function ( path , obj ) {
545
+ // Convert to '.$' to check subpaths re: gh-6405
546
+ const cleanPath = path . replace ( / \. \d + \. / g, '.$.' ) . replace ( / \. \d + $ / , '.$' ) ;
545
547
if ( obj === undefined ) {
546
548
if ( this . paths . hasOwnProperty ( path ) ) {
547
549
return this . paths [ path ] ;
548
550
}
549
- if ( this . subpaths . hasOwnProperty ( path ) ) {
550
- return this . subpaths [ path ] ;
551
+ if ( this . subpaths . hasOwnProperty ( cleanPath ) ) {
552
+ return this . subpaths [ cleanPath ] ;
551
553
}
552
- if ( this . singleNestedPaths . hasOwnProperty ( path ) ) {
553
- return this . singleNestedPaths [ path ] ;
554
+ if ( this . singleNestedPaths . hasOwnProperty ( cleanPath ) ) {
555
+ return this . singleNestedPaths [ cleanPath ] ;
554
556
}
555
557
556
558
// Look for maps
@@ -628,6 +630,10 @@ Schema.prototype.path = function(path, obj) {
628
630
this . singleNestedPaths [ path + '.' + key ] =
629
631
schemaType . schema . singleNestedPaths [ key ] ;
630
632
}
633
+ for ( const key in schemaType . schema . subpaths ) {
634
+ this . singleNestedPaths [ path + '.' + key ] =
635
+ schemaType . schema . subpaths [ key ] ;
636
+ }
631
637
632
638
Object . defineProperty ( schemaType . schema , 'base' , {
633
639
configurable : true ,
@@ -656,6 +662,29 @@ Schema.prototype.path = function(path, obj) {
656
662
} ) ;
657
663
}
658
664
665
+ if ( schemaType . $isMongooseArray && schemaType . caster instanceof SchemaType ) {
666
+ let arrayPath = path ;
667
+ let _schemaType = schemaType ;
668
+
669
+ let toAdd = [ ] ;
670
+ while ( _schemaType . $isMongooseArray ) {
671
+ arrayPath = arrayPath + '.$' ;
672
+
673
+ // Skip arrays of document arrays
674
+ if ( _schemaType . $isMongooseDocumentArray ) {
675
+ toAdd = [ ] ;
676
+ break ;
677
+ }
678
+ _schemaType = _schemaType . caster . clone ( ) ;
679
+ _schemaType . path = arrayPath ;
680
+ toAdd . push ( _schemaType ) ;
681
+ }
682
+
683
+ for ( const _schemaType of toAdd ) {
684
+ this . subpaths [ _schemaType . path ] = _schemaType ;
685
+ }
686
+ }
687
+
659
688
return this ;
660
689
} ;
661
690
@@ -939,6 +968,9 @@ Schema.prototype.indexedPaths = function indexedPaths() {
939
968
*/
940
969
941
970
Schema . prototype . pathType = function ( path ) {
971
+ // Convert to '.$' to check subpaths re: gh-6405
972
+ const cleanPath = path . replace ( / \. \d + \. / g, '.$.' ) . replace ( / \. \d + $ / , '.$' ) ;
973
+
942
974
if ( this . paths . hasOwnProperty ( path ) ) {
943
975
return 'real' ;
944
976
}
@@ -948,10 +980,10 @@ Schema.prototype.pathType = function(path) {
948
980
if ( this . nested . hasOwnProperty ( path ) ) {
949
981
return 'nested' ;
950
982
}
951
- if ( this . subpaths . hasOwnProperty ( path ) ) {
983
+ if ( this . subpaths . hasOwnProperty ( cleanPath ) ) {
952
984
return 'real' ;
953
985
}
954
- if ( this . singleNestedPaths . hasOwnProperty ( path ) ) {
986
+ if ( this . singleNestedPaths . hasOwnProperty ( cleanPath ) ) {
955
987
return 'real' ;
956
988
}
957
989
@@ -1080,7 +1112,7 @@ Schema.prototype.setupTimestamp = function(timestamps) {
1080
1112
} ;
1081
1113
1082
1114
/*!
1083
- * ignore
1115
+ * ignore. Deprecated re: #6405
1084
1116
*/
1085
1117
1086
1118
function getPositionalPathType ( self , path ) {
0 commit comments