From 5740c2cf2c9785265c20472024afa119068644d1 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 27 Oct 2022 14:10:43 -0400 Subject: [PATCH 1/2] fix(schema): make _getSchema() find path underneath subdocument with map of mixed Fix #12530 --- lib/schema.js | 11 +++++++++-- test/schema.test.js | 13 +++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/schema.js b/lib/schema.js index ba8435e0e66..4a3f68e03ae 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -2409,8 +2409,15 @@ Schema.prototype._getSchema = function(path) { if (p + 1 >= parts.length) { return foundschema.$__schemaType; } - const ret = search(parts.slice(p + 1), foundschema.$__schemaType.schema); - return ret; + + if (foundschema.$__schemaType instanceof MongooseTypes.Mixed) { + return foundschema.$__schemaType; + } + if (foundschema.$__schemaType.schema != null) { + // Map of docs + const ret = search(parts.slice(p + 1), foundschema.$__schemaType.schema); + return ret; + } } foundschema.$fullPath = resultPath.join('.'); diff --git a/test/schema.test.js b/test/schema.test.js index 36368c58b2d..619df3dd7c9 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -2885,4 +2885,17 @@ describe('schema', function() { assert.equal(schema.path('num').instance, 'Decimal128'); assert.equal(schema.path('num2').instance, 'Decimal128'); }); + + it('_getSchema finds path underneath nested subdocument with map of mixed (gh-12530)', function() { + const schema = new Schema({ + child: new Schema({ + testMap: { + type: Map, + of: 'Mixed' + } + }) + }); + + assert.equal(schema._getSchema('child.testMap.foo.bar').instance, 'Mixed'); + }); }); From 0b70933e71d2fb7ba16baa502c8bcbeb4e9d28d6 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 27 Oct 2022 14:33:24 -0400 Subject: [PATCH 2/2] fix(populate): correctly get schema type underneath map of mixed in getSchemaTypes --- lib/helpers/populate/getSchemaTypes.js | 2 ++ test/helpers/populate.getSchemaTypes.test.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/helpers/populate/getSchemaTypes.js b/lib/helpers/populate/getSchemaTypes.js index 177e7889344..0534f015286 100644 --- a/lib/helpers/populate/getSchemaTypes.js +++ b/lib/helpers/populate/getSchemaTypes.js @@ -173,6 +173,8 @@ module.exports = function getSchemaTypes(model, schema, doc, path) { } } } + } else if (foundschema.$isSchemaMap && foundschema.$__schemaType instanceof Mixed) { + return foundschema.$__schemaType; } const fullPath = nestedPath.concat([trypath]).join('.'); diff --git a/test/helpers/populate.getSchemaTypes.test.js b/test/helpers/populate.getSchemaTypes.test.js index 531290156e8..b34af9d3a36 100644 --- a/test/helpers/populate.getSchemaTypes.test.js +++ b/test/helpers/populate.getSchemaTypes.test.js @@ -178,4 +178,18 @@ describe('getSchemaTypes', function() { assert.equal(schemaTypes.length, 1); assert.equal(schemaTypes[0].options.ref, 'Enemy'); }); + + it('finds path underneath nested subdocument with map of mixed (gh-12530)', function() { + const schema = new Schema({ + child: new Schema({ + testMap: { + type: Map, + of: 'Mixed' + } + }) + }); + + const schemaTypes = getSchemaTypes(null, schema, null, 'child.testMap.foo.bar'); + assert.equal(schemaTypes.instance, 'Mixed'); + }); });