From 8066b145c07984c8b7e56dbb51721c0a3d48e18a Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 15 Jan 2016 14:15:03 -0500 Subject: [PATCH] fix(types): smarter casting for buffer with number (Fix #3764) --- lib/schema/buffer.js | 3 +++ test/types.buffer.test.js | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/schema/buffer.js b/lib/schema/buffer.js index d26cc339b7c..b64234fce89 100644 --- a/lib/schema/buffer.js +++ b/lib/schema/buffer.js @@ -125,6 +125,9 @@ SchemaBuffer.prototype.cast = function(value, doc, init) { var type = typeof value; if ('string' == type || 'number' == type || Array.isArray(value)) { + if (type === 'number') { + value = [value]; + } ret = new MongooseBuffer(value, [this.path, doc]); return ret; } diff --git a/test/types.buffer.test.js b/test/types.buffer.test.js index 74af5701885..b7081c387d3 100644 --- a/test/types.buffer.test.js +++ b/test/types.buffer.test.js @@ -450,5 +450,14 @@ describe('types.buffer', function() { }); }); }); + + it('cast from number (gh-3764)', function(done) { + var schema = new Schema({ buf: Buffer }); + var MyModel = mongoose.model('gh3764', schema); + + var doc = new MyModel({ buf: 9001 }); + assert.equal(doc.buf.length, 1); + done(); + }); }); });