Skip to content

Commit

Permalink
fix(types): smarter casting for buffer with number (Fix #3764)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 15, 2016
1 parent 823d43a commit 8066b14
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/schema/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions test/types.buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit 8066b14

Please # to comment.