Permalink
Browse files

fix: select non-null fields for composition

  • Loading branch information...
jimlambie committed Jan 31, 2017
1 parent 0d2398c commit 21e48bf271353d134e896920ba9d28be117cbd99
@@ -35,7 +35,7 @@ Composer.prototype.composeOne = function (doc, callback) {
var schema = self.model.schema

var composable = Object.keys(schema).filter(function (key) {
return schema[key].type === 'Reference' && typeof doc[key] !== 'undefined'
return schema[key].type === 'Reference' && doc[key] && typeof doc[key] !== 'undefined'
})

if (_.isEmpty(composable)) return callback(doc)
@@ -131,6 +131,57 @@ describe('Reference Field', function () {
});
});

it('should populate all reference fields that aren\'t null', function (done) {

// first person
var gertrude = { name: 'Gertrude Stein' }

config.set('query.useVersionFilter', true)

var client = request(connectionString);
client
.post('/v1/library/person')
.set('Authorization', 'Bearer ' + bearerToken)
.send(gertrude)
.expect(200)
.end(function (err, res) {
if (err) return done(err);

var personId = res.body.results[0]._id

var ernest = {
name: 'Ernest Hemingway',
spouse: null,
friend: personId.toString()
}

client
.post('/v1/library/person')
.set('Authorization', 'Bearer ' + bearerToken)
.send(ernest)
.expect(200)
.end(function (err, res) {
if (err) return done(err)

client
.get('/v1/library/person?filter={"name":"Ernest Hemingway", "friend":{"$ne":null}}&compose=true')
.set('Authorization', 'Bearer ' + bearerToken)
.expect(200)
.end(function (err, res) {
if (err) return done(err);

should.exist(res.body.results)
var result = res.body.results[0]

should.exist(result.friend)
result.friend.name.should.eql('Gertrude Stein')

done();
})
})
});
});

it('should return results for a reference field containing an Array of Strings', function (done) {

var person = { name: 'Ernest Hemingway' };
@@ -18,6 +18,9 @@
},
"spouse": {
"type": "Reference"
},
"friend": {
"type": "Reference"
}
},
"settings": {

0 comments on commit 21e48bf

Please sign in to comment.