Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly find paths underneath single nested document with an array of mixed #12605

Merged
merged 2 commits into from Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/helpers/populate/getSchemaTypes.js
Expand Up @@ -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('.');
Expand Down
11 changes: 9 additions & 2 deletions lib/schema.js
Expand Up @@ -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('.');
Expand Down
14 changes: 14 additions & 0 deletions test/helpers/populate.getSchemaTypes.test.js
Expand Up @@ -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');
});
});
13 changes: 13 additions & 0 deletions test/schema.test.js
Expand Up @@ -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');
});
});