Skip to content

Commit

Permalink
fix emberobserver issue for missing data member in payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Jul 23, 2018
1 parent 5de92cc commit 850bdf2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default class Relationship {
this.canonicalMembers = new OrderedSet();
this.store = store;
this.key = relationshipMeta.key;
this.kind = relationshipMeta.kind;
this.inverseKey = inverseKey;
this.internalModel = internalModel;
this.isAsync = typeof async === 'undefined' ? true : async;
Expand Down Expand Up @@ -526,7 +527,7 @@ export default class Relationship {
warn(
`You pushed a record of type '${this.internalModel.modelName}' with a relationship '${
this.key
}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload.`,
}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload. EmberData will treat this relationship as known-to-be-empty.`,
this.isAsync || this.hasAnyRelationshipData,
{
id: 'ds.store.push-link-for-sync-relationship',
Expand Down Expand Up @@ -696,6 +697,11 @@ export default class Relationship {
this.updateData(payload.data, initial);
} else if (payload._partialData !== undefined) {
this.updateData(payload._partialData, initial);
} else if (this.isAsync === false) {
hasRelationshipDataProperty = true;
let data = this.kind === 'hasMany' ? [] : null;

this.updateData(data, initial);
}

if (payload.links && payload.links.related) {
Expand Down
2 changes: 1 addition & 1 deletion addon/-legacy-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3162,7 +3162,7 @@ function setupRelationships(store, internalModel, data, modelNameToInverseMap) {
warn(
`You pushed a record of type '${
internalModel.modelName
}' with a relationship '${relationshipName}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload.`,
}' with a relationship '${relationshipName}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload. EmberData will treat this relationship as known-to-be-empty.`,
isAsync || relationshipData.data,
{
id: 'ds.store.push-link-for-sync-relationship',
Expand Down
2 changes: 1 addition & 1 deletion addon/-record-data-private/system/model/model-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default class ModelData {
warn(
`You pushed a record of type '${
this.modelName
}' with a relationship '${relationshipName}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload.`,
}' with a relationship '${relationshipName}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload. EmberData will treat this relationship as known-to-be-empty.`,
isAsync || relationshipData.data,
{
id: 'ds.store.push-link-for-sync-relationship',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default class Relationship {
constructor(store, inverseKey, relationshipMeta, modelData, inverseIsAsync) {
heimdall.increment(newRelationship);
this.inverseIsAsync = inverseIsAsync;
this.kind = relationshipMeta.kind;
let async = relationshipMeta.options.async;
let polymorphic = relationshipMeta.options.polymorphic;
this.modelData = modelData;
Expand Down Expand Up @@ -572,7 +573,7 @@ export default class Relationship {
warn(
`You pushed a record of type '${this.modelData.modelName}' with a relationship '${
this.key
}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload.`,
}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload. EmberData will treat this relationship as known-to-be-empty.`,
this.isAsync || this.hasAnyRelationshipData,
{
id: 'ds.store.push-link-for-sync-relationship',
Expand Down Expand Up @@ -647,6 +648,11 @@ export default class Relationship {
if (payload.data !== undefined) {
hasRelationshipDataProperty = true;
this.updateData(payload.data, initial);
} else if (this.isAsync === false) {
hasRelationshipDataProperty = true;
let data = this.kind === 'hasMany' ? [] : null;

this.updateData(data, initial);
}

if (payload.links && payload.links.related) {
Expand Down
90 changes: 90 additions & 0 deletions tests/integration/relationships/json-api-links-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ module('integration/relationship/json-api-links | Relationship fetching', {
const Pet = Model.extend({
name: attr(),
owner: belongsTo('user', { async: false, inverse: 'pets' }),
friends: hasMany('pet', { async: false, inverse: 'friends' }),
});
const Adapter = JSONAPIAdapter.extend();

Expand Down Expand Up @@ -1965,3 +1966,92 @@ test('We should not fetch a hasMany relationship with links that we know is empt
'We improperly fetched the link for a previously fetched and found to be empty relationship';
run(() => user2.get('pets'));
});

test('We should not fetch a sync hasMany relationship with a link that is missing the data member', function(assert) {
assert.expect(1);
let { store, adapter } = env;

let petPayload = {
data: {
type: 'pet',
id: '1',
attributes: {
name: 'Shen',
},
relationships: {
friends: {
links: {
related: './shen/friends',
},
},
},
},
};

adapter.shouldBackgroundReloadRecord = () => false;
adapter.findRecord = () => {
assert.ok(false, 'We should not call findRecord');
};
adapter.findMany = () => {
assert.ok(false, 'We should not call findMany');
};
adapter.findHasMany = () => {
assert.ok(false, 'We should not call findHasMany');
};
adapter.findBelongsTo = () => {
assert.ok(false, 'We should not call findBelongsTo');
};

// setup users
let shen = run(() => store.push(petPayload));

// should not fire a request
run(() => shen.get('pets'));

assert.ok(true, 'We reached the end of the test');
});

test('We should not fetch a sync belongsTo relationship with a link that is missing the data member', function(assert) {
assert.expect(1);
let { store, adapter } = env;

let petPayload = {
data: {
type: 'pet',
id: '1',
attributes: {
name: 'Shen',
},
relationships: {
owner: {
links: {
related: './shen/owner',
self: './owner/a',
},
},
},
},
};

adapter.shouldBackgroundReloadRecord = () => false;
adapter.findRecord = () => {
assert.ok(false, 'We should not call findRecord');
};
adapter.findMany = () => {
assert.ok(false, 'We should not call findMany');
};
adapter.findHasMany = () => {
assert.ok(false, 'We should not call findHasMany');
};
adapter.findBelongsTo = () => {
assert.ok(false, 'We should not call findBelongsTo');
};

// setup users
let shen = run(() => store.push(petPayload));

// should not fire a request
run(() => shen.get('owner'));

assert.ok(true, 'We reached the end of the test');
});
2 changes: 1 addition & 1 deletion tests/unit/store/push-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ testInDebug(
},
});
});
}, /You pushed a record of type 'person' with a relationship 'phoneNumbers' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload./);
}, /You pushed a record of type 'person' with a relationship 'phoneNumbers' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload. EmberData will treat this relationship as known-to-be-empty./);
}
);

Expand Down

0 comments on commit 850bdf2

Please sign in to comment.