Skip to content

Commit 36d23ce

Browse files
committedSep 1, 2021
fix(schema): handle maps of maps
Fix #10644
1 parent d21d2b1 commit 36d23ce

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed
 

‎lib/schema.js

+34-14
Original file line numberDiff line numberDiff line change
@@ -681,21 +681,9 @@ Schema.prototype.path = function(path, obj) {
681681
// The '$' is to imply this path should never be stored in MongoDB so we
682682
// can easily build a regexp out of this path, and '*' to imply "any key."
683683
const mapPath = path + '.$*';
684-
let _mapType = { type: {} };
685-
if (utils.hasUserDefinedProperty(obj, 'of')) {
686-
const isInlineSchema = utils.isPOJO(obj.of) &&
687-
Object.keys(obj.of).length > 0 &&
688-
!utils.hasUserDefinedProperty(obj.of, this.options.typeKey);
689-
_mapType = isInlineSchema ? new Schema(obj.of) : obj.of;
690-
}
691-
if (utils.hasUserDefinedProperty(obj, 'ref')) {
692-
_mapType = { type: _mapType, ref: obj.ref };
693-
}
694684

695-
this.paths[mapPath] = this.interpretAsType(mapPath,
696-
_mapType, this.options);
685+
this.paths[mapPath] = schemaType.$__schemaType;
697686
this.mapPaths.push(this.paths[mapPath]);
698-
schemaType.$__schemaType = this.paths[mapPath];
699687
}
700688

701689
if (schemaType.$isSingleNested) {
@@ -1032,9 +1020,41 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
10321020
'http://bit.ly/mongoose-schematypes for a list of valid schema types.');
10331021
}
10341022

1035-
return new MongooseTypes[name](path, obj);
1023+
const schemaType = new MongooseTypes[name](path, obj);
1024+
1025+
if (schemaType.$isSchemaMap) {
1026+
createMapNestedSchemaType(this, schemaType, path, obj, options);
1027+
}
1028+
1029+
return schemaType;
10361030
};
10371031

1032+
/*!
1033+
* ignore
1034+
*/
1035+
1036+
function createMapNestedSchemaType(schema, schemaType, path, obj, options) {
1037+
const mapPath = path + '.$*';
1038+
let _mapType = { type: {} };
1039+
if (utils.hasUserDefinedProperty(obj, 'of')) {
1040+
const isInlineSchema = utils.isPOJO(obj.of) &&
1041+
Object.keys(obj.of).length > 0 &&
1042+
!utils.hasUserDefinedProperty(obj.of, schema.options.typeKey);
1043+
if (isInlineSchema) {
1044+
_mapType = { [schema.options.typeKey]: new Schema(obj.of) };
1045+
} else if (utils.isPOJO(obj.of)) {
1046+
_mapType = Object.assign({}, obj.of);
1047+
} else {
1048+
_mapType = { [schema.options.typeKey]: obj.of };
1049+
}
1050+
1051+
if (utils.hasUserDefinedProperty(obj, 'ref')) {
1052+
_mapType.ref = obj.ref;
1053+
}
1054+
}
1055+
schemaType.$__schemaType = schema.interpretAsType(mapPath, _mapType, options);
1056+
}
1057+
10381058
/**
10391059
* Iterates the schemas paths similar to Array#forEach.
10401060
*

0 commit comments

Comments
 (0)
Please sign in to comment.