Skip to content

Commit

Permalink
fix stack_overflow from adhoc test
Browse files Browse the repository at this point in the history
  • Loading branch information
aheckmann committed Nov 2, 2011
1 parent fe7cbae commit 594d542
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/document.js
Expand Up @@ -304,10 +304,10 @@ Document.prototype.set = function (path, val, type) {
if (!pathToMark) pathToMark = path;
}


if ((!schema || null === val || undefined === val) ||
this.try(function(){
val = schema.applySetters(schema.cast(val, self), self);
var casted = schema.cast(val, self, false, self.get(path));
val = schema.applySetters(casted, self);
})) {

if (this.isNew) {
Expand Down
10 changes: 3 additions & 7 deletions lib/schema/documentarray.js
Expand Up @@ -97,16 +97,12 @@ DocumentArray.prototype.doValidate = function (array, fn, scope) {
* @api private
*/

DocumentArray.prototype.cast = function (value, doc, init) {
DocumentArray.prototype.cast = function (value, doc, init, prev) {
var subdoc
, cur
, i

if (Array.isArray(value)) {
if (!(value instanceof MongooseDocumentArray)) {
// get the current MongooseArray if any before
// casting the value to prevent infinite loop.
cur = doc && doc.get(this.path);
value = new MongooseDocumentArray(value, this.path, doc);
}

Expand All @@ -120,7 +116,7 @@ DocumentArray.prototype.cast = function (value, doc, init) {
delete subdoc._doc._id;
value[i] = subdoc.init(value[i]);
} else {
subdoc = cur && cur.id(value[i]._id) ||
subdoc = prev && prev.id(value[i]._id) ||
new this.caster(null, value);
value[i] = subdoc.set(value[i]);
}
Expand All @@ -129,7 +125,7 @@ DocumentArray.prototype.cast = function (value, doc, init) {

return value;
} else {
return this.cast([value], doc, init);
return this.cast([value], doc, init, prev);
}

throw new CastError('documentarray', value);
Expand Down

0 comments on commit 594d542

Please sign in to comment.