Skip to content

Commit

Permalink
Use modelNameFromPayloadKey when type is given
Browse files Browse the repository at this point in the history
The code checked for a model using the possibly-overridden
modelNameFromPayloadKey but didn’t use the override when
getting the serialiser and model.
  • Loading branch information
backspace committed Feb 28, 2016
1 parent 7348817 commit aca7096
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions addon/serializers/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ var RESTSerializer = JSONSerializer.extend({
const primaryHasTypeAttribute = modelHasAttributeOrRelationshipNamedType(primaryModelClass);
// Support polymorphic records in async relationships
if (!primaryHasTypeAttribute && hash.type && store._hasModelFor(this.modelNameFromPayloadKey(hash.type))) {
serializer = store.serializerFor(hash.type);
modelClass = store.modelFor(hash.type);
serializer = store.serializerFor(this.modelNameFromPayloadKey(hash.type));
modelClass = store.modelFor(this.modelNameFromPayloadKey(hash.type));
} else {
serializer = primarySerializer;
modelClass = primaryModelClass;
Expand Down
29 changes: 29 additions & 0 deletions tests/integration/serializers/rest-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,35 @@ test("normalizeResponse with custom modelNameFromPayloadKey", function(assert) {
});
});

test("normalizeResponse with type and custom modelNameFromPayloadKey", function(assert) {
assert.expect(1);

env.restSerializer.modelNameFromPayloadKey = function(root) {
return Ember.String.singularize(root);
};

var jsonHash = {
"home-planets": [{ id: "1", name: "Umber", type: "home-planets" }]
};
var array;

run(function() {
array = env.restSerializer.normalizeResponse(env.store, HomePlanet, jsonHash, '1', 'findAll');
});

assert.deepEqual(array, {
data: [{
id: '1',
type: 'home-planet',
attributes: {
name: 'Umber'
},
relationships: {}
}],
included: []
});
});

test("normalizeResponse warning with custom modelNameFromPayloadKey", function(assert) {
var homePlanet;
var oldModelNameFromPayloadKey = env.restSerializer.modelNameFromPayloadKey;
Expand Down

0 comments on commit aca7096

Please sign in to comment.