Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard for embedded unknown hasMany relationship #3126

Merged
merged 1 commit into from
Jun 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/ember-data/lib/serializers/embedded-records-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,17 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
}
var includeIds = this.hasSerializeIdsOption(attr);
var includeRecords = this.hasSerializeRecordsOption(attr);
var key;
var key, hasMany;
if (includeIds) {
key = this.keyForRelationship(attr, relationship.kind, 'serialize');
json[key] = snapshot.hasMany(attr, { ids: true });
} else if (includeRecords) {
key = this.keyForAttribute(attr, 'serialize');
json[key] = snapshot.hasMany(attr).map(function(embeddedSnapshot) {
hasMany = snapshot.hasMany(attr);

Ember.warn("The embedded relationship '" + key + "' is undefined for '" + snapshot.modelName + "' with id '" + snapshot.id + "'. Please include it in your original payload.", Ember.typeOf(hasMany) !== 'undefined');

json[key] = Ember.A(hasMany).map(function(embeddedSnapshot) {
var embeddedJson = embeddedSnapshot.record.serialize({ includeId: true });
this.removeEmbeddedForeignKey(snapshot, embeddedSnapshot, relationship, embeddedJson);
return embeddedJson;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,32 @@ test("serialize with embedded objects (hasMany relationship)", function() {
});
});

test("serialize with embedded objects (unknown hasMany relationship)", function() {
var league;
run(function() {
league = env.store.push(HomePlanet, { name: "Villain League", id: "123" });
});

env.registry.register('serializer:home-planet', DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
villains: { embedded: 'always' }
}
}));

var serializer, json;
warns(function() {
run(function() {
serializer = env.container.lookup("serializer:home-planet");
json = serializer.serialize(league._createSnapshot());
});
}, /The embedded relationship 'villains' is undefined for 'home-planet' with id '123'. Please include it in your original payload./);

deepEqual(json, {
name: "Villain League",
villains: []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really what we want? (since the relationship is "unknown")

For other serializers it would make sense to exclude unknown relationships from the payload to prevent overwriting. Might not be necessary for embedded since it should be present, c?

ping @igorT @jakesjews

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree that an embedded relationship should always be present but a developer might not necessarily be in control of the API. An alternative could perhaps be an assert so it's easier to debug.

});
});

test("serialize with embedded objects (hasMany relationship) supports serialize:false", function() {
run(function() {
league = env.store.createRecord(HomePlanet, { name: "Villain League", id: "123" });
Expand Down