Skip to content

Commit

Permalink
fixed; do not apply setters when doc returned from db
Browse files Browse the repository at this point in the history
closes #390
  • Loading branch information
aheckmann committed Aug 3, 2011
1 parent 50816ae commit 35e8da1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
9 changes: 6 additions & 3 deletions lib/mongoose/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ Document.prototype.buildDoc = function () {
};

/**
* Inits (hydrates) the document.
* Inits (hydrates) the document without setters.
*
* Called internally after a document is returned
* from mongodb.
*
* @param {Object} document returned by mongo
* @api private
Expand Down Expand Up @@ -127,7 +130,7 @@ Document.prototype.init = function (doc, fn) {
} else if (obj[i] !== undefined) {
if (schema) {
self.try(function(){
doc[i] = schema.applySetters(schema.cast(obj[i], self), self);
doc[i] = schema.cast(obj[i], self);
});
} else {
doc[i] = obj[i];
Expand Down Expand Up @@ -255,7 +258,7 @@ Document.prototype.set = function (path, val, type) {

if ((!schema || null === val || undefined === val) ||
this.try(function(){
val = schema.applySetters(schema.cast(val, self), self);
val = schema.applySetters(schema.cast(val, self, true), self);
})) {

var priorVal = this.get(path);
Expand Down
16 changes: 10 additions & 6 deletions lib/mongoose/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ DocumentArray.prototype.doValidate = function (array, fn, scope) {
var self = this;
SchemaType.prototype.doValidate.call(this, array, function(err){
if (err) return fn(err);

var count = array.length
, error = false;

Expand Down Expand Up @@ -94,16 +94,20 @@ DocumentArray.prototype.doValidate = function (array, fn, scope) {
* @api private
*/

DocumentArray.prototype.cast = function (value, doc) {
if (Array.isArray(value)){
DocumentArray.prototype.cast = function (value, doc, useSet) {
if (Array.isArray(value)) {
if (!(value instanceof MongooseDocumentArray))
value = new MongooseDocumentArray(value, this.path, doc);

for (var i = 0, l = value.length; i < l; i++)
if (!(value[i] instanceof Subdocument)){
for (var i = 0, l = value.length; i < l; i++) {
if (!(value[i] instanceof Subdocument)) {
var doc = new this.caster(null, value);
value[i] = doc.init(value[i].doc || value[i]);

value[i] = useSet
? doc.set (value[i].doc || value[i])
: doc.init(value[i].doc || value[i]);
}
}

return value;
} else {
Expand Down
45 changes: 30 additions & 15 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3446,17 +3446,13 @@ module.exports = {
});

// Don't know how to test those on a embedded document.
/*
//EmbeddedSchema.post('init', function () {
//init = true;
//});

EmbeddedSchema.post('init', function () {
init = true;
});
EmbeddedSchema.post('remove', function () {
remove = true;
});
*/
//EmbeddedSchema.post('remove', function () {
//remove = true;
//});

mongoose.model('Parent', ParentSchema);

Expand Down Expand Up @@ -3629,15 +3625,22 @@ module.exports = {
});
},

// GH-365
// GH-365, GH-390, GH-422
'test that setters are used on embedded documents': function () {
var db = start();

function setLat (val) {
return parseInt(val);
}

var tick = 0;
function uptick () {
return ++tick;
}

var Location = new Schema({
lat: {type: Number, default: 0, set: setLat}
lat: { type: Number, default: 0, set: setLat}
, long: { type: Number, set: uptick }
});

var Deal = new Schema({
Expand All @@ -3648,12 +3651,24 @@ module.exports = {
Location = db.model('Location', Location, 'locations_' + random());
Deal = db.model('Deal', Deal, 'deals_' + random());

var location = new Location({lat: 1.2});
var location = new Location({lat: 1.2, long: 10});
location.lat.valueOf().should.equal(1);
location.long.valueOf().should.equal(1);

var deal = new Deal({title: "My deal", locations: [{lat: 1.2}]});
var deal = new Deal({title: "My deal", locations: [{lat: 1.2, long: 33}]});
deal.locations[0].lat.valueOf().should.equal(1);
db.close();
deal.locations[0].long.valueOf().should.equal(2);

deal.save(function (err) {
should.strictEqual(err, null);
Deal.findById(deal._id, function (err, deal) {
db.close();
should.strictEqual(err, null);
deal.locations[0].lat.valueOf().should.equal(1);
// GH-422
deal.locations[0].long.valueOf().should.equal(2);
});
});
},

// GH-289
Expand Down

0 comments on commit 35e8da1

Please sign in to comment.