Skip to content

Commit

Permalink
Fix crash w/ polymorphic errors in JSONSerializer
Browse files Browse the repository at this point in the history
Use a specialized version of normalizeRelationships for errors rather
than relying on the regular one that expects a `type` property.
  • Loading branch information
Jimmy Bourassa committed May 11, 2015
1 parent 429d9eb commit 6f8ede2
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/ember-data/lib/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Serializer from "ember-data/system/serializer";

var get = Ember.get;
var isNone = Ember.isNone;
var isArray = Ember.isArray;
var map = Ember.ArrayPolyfills.map;
var forEach = Ember.ArrayPolyfills.forEach;
var merge = Ember.merge;

/**
Expand Down Expand Up @@ -274,10 +276,41 @@ export default Serializer.extend({
normalizeErrors: function(typeClass, hash) {
this.normalizeId(hash);
this.normalizeAttributes(typeClass, hash);
this.normalizeRelationships(typeClass, hash);
this.normalizeRelationshipErrors(typeClass, hash);
this.normalizeUsingDeclaredMapping(typeClass, hash);
},

/**
@method normalizeRelationshipErrors
@private
*/
normalizeRelationshipErrors: function(typeClass, hash) {
if (!this.keyForRelationship) {
return;
}

typeClass.eachRelationship(function(key, relationship) {
var payloadKey = this.keyForRelationship(key, relationship.kind);
if (key !== payloadKey && hash.hasOwnProperty(payloadKey)) {
hash[key] = hash[payloadKey];
delete hash[payloadKey];
}

if (!hash || !hash[key]) {
return;
}
if (relationship.kind === 'hasMany' && isArray(hash[key])) {
forEach.call(hash[key], function(errors) {
if (errors) {
this.normalizeErrors(relationship.type, errors);
}
}, this);
} else {
this.normalizeErrors(relationship.type, hash[key]);
}
}, this);
},

/**
Looks up the property key that was set by the custom `attr` mapping
passed to the serializer.
Expand Down

0 comments on commit 6f8ede2

Please sign in to comment.