Skip to content

Commit

Permalink
Merge pull request #4908 from bmac/warn-for-4906
Browse files Browse the repository at this point in the history
Add a better error message when malformatted JSON API is pushed into …
  • Loading branch information
stefanpenner authored Apr 5, 2017
2 parents 2015435 + f78d498 commit ac2dbe0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions addon/-private/system/relationships/state/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export default class BelongsToRelationship extends Relationship {
}

updateData(data, initial) {
assert(`Ember Data expected the data for the ${this.key} relationship on a ${this.internalModel.toString()} to be in a JSON API format and include an \`id\` and \`type\` property but it found ${Ember.inspect(data)}. Please check your serializer and make sure it is serializing the relationship payload into a JSON API format.`, data === null || data.id !== undefined && data.type !== undefined);
let internalModel = this.store._pushResourceIdentifier(this, data);
if (initial) {
this.setInitialCanonicalInternalModel(internalModel);
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/relationships/belongs-to-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1364,3 +1364,22 @@ test("A belongsTo relationship can be reloaded using a reference if it was fetch
});
});
});

testInDebug("A belongsTo relationship warns if malformatted data is pushed into the store", function(assert) {
assert.expectAssertion(function() {
run(function() {
let chapter = env.store.push({
data: {
type: 'chapter',
id: 1,
relationships: {
book: {
data: { id: 1, name: 'The Gallic Wars' }
}
}
}
});
chapter.get('book');
});
}, /expected the data for the book relationship on a <chapter:1> to be in a JSON API format/);
});

0 comments on commit ac2dbe0

Please sign in to comment.