Skip to content

Commit

Permalink
Merge pull request #5178 from lukemelia/ignore-unknown-jsonapi-resour…
Browse files Browse the repository at this point in the history
…ce-types

Ignore included resources without that don't have a corresponding ember-data model
  • Loading branch information
bmac authored Sep 28, 2017
2 parents d927547 + 056a1c9 commit 16e1670
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
8 changes: 5 additions & 3 deletions addon/serializers/json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ const JSONAPISerializer = JSONSerializer.extend({
}

if (Array.isArray(documentHash.included)) {
let ret = new Array(documentHash.included.length);

let ret = new Array();
for (let i = 0; i < documentHash.included.length; i++) {
let included = documentHash.included[i];
ret[i] = this._normalizeResourceHelper(included);
let normalized = this._normalizeResourceHelper(included);
if (normalized !== null) { // can be null when unknown type is encountered
ret.push(normalized);
}
}

documentHash.included = ret;
Expand Down
65 changes: 65 additions & 0 deletions tests/integration/serializers/json-api-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,71 @@ testInDebug('Warns when normalizing an unknown type', function(assert) {
}, /Encountered a resource object with type "UnknownType", but no model was found for model name "unknown-type"/);
});

testInDebug('Warns when normalizing payload with unknown type included', function(assert) {
var documentHash = {
data: {
type: 'users',
id: '1',
attributes: {
'first-name': 'Yehuda',
'last-name': 'Katz'
},
relationships: {
company: {
data: { type: 'unknown-types', id: '2' }
}
}
},
included: [{
type: 'unknown-types',
id: '2',
attributes: {
name: 'WyKittens'
}
}]
};

assert.expectWarning(function() {
run(function() {
env.store.serializerFor('user').normalizeResponse(env.store, User, documentHash, '1', 'findRecord');
});
}, /Encountered a resource object with type "unknown-types", but no model was found for model name "unknown-type"/);
});

testInDebug('Warns but does not fail when pushing payload with unknown type included', function(assert) {
var documentHash = {
data: {
type: 'users',
id: '1',
attributes: {
'first-name': 'Yehuda',
'last-name': 'Katz'
},
relationships: {
company: {
data: { type: 'unknown-types', id: '2' }
}
}
},
included: [{
type: 'unknown-types',
id: '2',
attributes: {
name: 'WyKittens'
}
}]
};

assert.expectWarning(function() {
run(function() {
env.store.pushPayload(documentHash);
});
}, /Encountered a resource object with type "unknown-types", but no model was found for model name "unknown-type"/);

var user = store.peekRecord('user', 1);
assert.equal(get(user, 'firstName'), 'Yehuda', 'firstName is correct');
});

testInDebug('Warns when normalizing with type missing', function(assert) {
var documentHash = {
data: {
Expand Down

0 comments on commit 16e1670

Please sign in to comment.