diff --git a/lib/schema.js b/lib/schema.js index d94565e21bb..cbc1e80bd99 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -875,7 +875,9 @@ Object.defineProperty(Schema.prototype, 'base', { Schema.prototype.interpretAsType = function(path, obj, options) { if (obj instanceof SchemaType) { - return obj; + const clone = obj.clone(); + clone.path = path; + return clone; } // If this schema has an associated Mongoose object, use the Mongoose object's diff --git a/test/schema.test.js b/test/schema.test.js index 8b3ceafb214..1664f99d0a9 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -2471,4 +2471,28 @@ describe('schema', function() { assert.equal(schema.path('arr').caster.instance, 'Mixed'); }); + + it('handles using a schematype when defining a path (gh-9370)', function() { + const schema1 = Schema({ + foo: { + type: Number, + min: 4, + get: get + } + }); + + function get(v) { + return Math.floor(v); + } + + const schema2 = Schema({ + bar: schema1.path('foo') + }); + + const schematype = schema2.path('bar'); + assert.equal(schematype.path, 'bar'); + assert.equal(schematype.options.type, Number); + assert.equal(schematype.options.min, 4); + assert.equal(schematype.options.get, get); + }); });