Skip to content

Commit

Permalink
Merge pull request #1839 from bookshelf/rg-empty-hasone
Browse files Browse the repository at this point in the history
Empty hasOne relation should return null instead of {} when serialized
  • Loading branch information
ricardograca committed May 10, 2018
2 parents 13ee6f5 + 21e4aa3 commit 73b47dc
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
5 changes: 3 additions & 2 deletions lib/eager.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ class EagerRelation extends EagerBase {

// Handles the eager load for both the `morphTo` and regular cases.
_eagerLoadHelper(response, relationName, handled, options) {
const relatedModels = this.pushModels(relationName, handled, response);
const relatedData = handled.relatedData;
const relatedData = handled.relatedData;
const isEmptyHasOne = response.length === 0 && relatedData.type === 'hasOne';
const relatedModels = isEmptyHasOne ? [] : this.pushModels(relationName, handled, response);

return Promise.try(() => {
// If there is a response, fetch additional nested eager relations, if any.
Expand Down
6 changes: 6 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ const BookshelfModel = ModelBase.extend({
* than `Target` model's `id` / `{@link Model#idAttribute idAttribute}`.
*
* @returns {Model}
*
* The return value will always be a model, even if the relation doesn't exist, but in that
* case the relation will be `null` when {@link Model#serialize serializing} the model.
*/
hasOne(Target, foreignKey, foreignKeyTarget) {
return this._relation('hasOne', Target, { foreignKey, foreignKeyTarget }).init(this);
Expand Down Expand Up @@ -194,6 +197,9 @@ const BookshelfModel = ModelBase.extend({
* than `Target` model's `id` / `{@link Model#idAttribute idAttribute}`.
*
* @returns {Model}
*
* The return value will always be a model, even if the relation doesn't exist, but in that
* case the relation will be `null` when {@link Model#serialize serializing} the model.
*/
belongsTo(Target, foreignKey, foreignKeyTarget) {
return this._relation('belongsTo', Target, { foreignKey, foreignKeyTarget }).init(this);
Expand Down
4 changes: 1 addition & 3 deletions lib/relation.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,7 @@ const Relation = RelationBase.extend({

// Fetches all `eagerKeys` from the current relation.
eagerKeys(response) {
const key = this.isInverse() && !this.isThrough()
? this.key('foreignKey')
: this.parentIdAttribute;
const key = this.isInverse() && !this.isThrough() ? this.key('foreignKey') : this.parentIdAttribute;
return _.reject(_(response).map(key).uniq().value(), _.isNil);
},

Expand Down
9 changes: 0 additions & 9 deletions test/integration/output/Relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,6 @@ module.exports = {
result: {
id: 3,
name: 'backbonejs.org',
meta: {

},
blogs: [],
authors: []
}
Expand All @@ -390,9 +387,6 @@ module.exports = {
result: {
id: 3,
name: 'backbonejs.org',
meta: {

},
authors: [],
blogs: []
}
Expand All @@ -401,9 +395,6 @@ module.exports = {
result: {
id: 3,
name: 'backbonejs.org',
meta: {

},
blogs: [],
authors: []
}
Expand Down
6 changes: 6 additions & 0 deletions test/integration/relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ module.exports = function(Bookshelf) {
}).then(checkTest(this));
});

it('does not load "hasOne" relationship when it doesn\'t exist (site -> meta)', function() {
return new Site({id: 3}).fetch({withRelated: ['meta']}).then(function(site) {
expect(site.toJSON()).to.not.have.property('meta')
});
});

it('eager loads "hasMany" relationships correctly (site -> authors, blogs)', function() {
return new Site({id: 1}).fetch({
withRelated: ['authors', 'blogs']
Expand Down

0 comments on commit 73b47dc

Please sign in to comment.