Don't coerce nested objects into Model instances

For queries, this is undesirable. It also affects
loopback-connector-mongodb's ability to perform nested queries (it
expects plain objects to work correctly, and to allow $elemMatch for
example).
This commit is contained in:
Fabien Franzen 2015-01-08 15:34:04 +01:00
parent cf0d9f730b
commit a24b222a72
2 changed files with 15 additions and 1 deletions

View File

@ -21,6 +21,7 @@ var setScopeValuesFromWhere = utils.setScopeValuesFromWhere;
var mergeQuery = utils.mergeQuery;
var util = require('util');
var assert = require('assert');
var Model = require('./model');
/**
* Base class for all persistent objects.
@ -593,6 +594,10 @@ DataAccessObject._coerce = function (where) {
continue;
}
if (DataType.prototype instanceof Model) {
continue;
}
if (DataType === geo.GeoPoint) {
// Skip the GeoPoint as the near operator breaks the assumption that
// an operation has only one property

View File

@ -7,13 +7,16 @@ describe('datatypes', function () {
before(function (done) {
db = getSchema();
Nested = db.define('Nested', {});
Model = db.define('Model', {
str: String,
date: Date,
num: Number,
bool: Boolean,
list: {type: [String]},
arr: Array
arr: Array,
nested: Nested
});
db.automigrate(function () {
Model.destroyAll(done);
@ -114,4 +117,10 @@ describe('datatypes', function () {
});
}
});
it('should not coerce nested objects into ModelConstructor types', function() {
var coerced = Model._coerce({ nested: { foo: 'bar' } });
coerced.nested.constructor.name.should.equal('Object');
});
});