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

[BUGFIX beta] serialize type for embedded, polymorphic belongsTo #3875

Merged
merged 1 commit into from
Nov 11, 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: 8 additions & 0 deletions packages/ember-data/lib/serializers/embedded-records-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ export default Ember.Mixin.create({
json[key] = null;
} else {
json[key] = embeddedSnapshot.id;

if (relationship.options.polymorphic) {
this.serializePolymorphicType(snapshot, json, relationship);
}
}
} else if (includeRecords) {
this._serializeEmbeddedBelongsTo(snapshot, json, relationship);
Expand All @@ -216,6 +220,10 @@ export default Ember.Mixin.create({
} else {
json[serializedKey] = embeddedSnapshot.record.serialize({ includeId: true });
this.removeEmbeddedForeignKey(snapshot, embeddedSnapshot, relationship, json[serializedKey]);

if (relationship.options.polymorphic) {
this.serializePolymorphicType(snapshot, json, relationship);
}
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,57 @@ test("serialize with embedded object (belongsTo relationship)", function() {
});
});

test("serialize with embedded object (polymorphic belongsTo relationship)", function() {
env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { embedded: 'always' }
}
}));

SuperVillain.reopen({
secretLab: DS.belongsTo('secret-lab', { polymorphic: true })
});

var json, tom;
run(function() {
tom = env.store.createRecord(
'super-villain',
{
id: "1",
firstName: "Tom",
lastName: "Dale",
secretLab: env.store.createRecord('bat-cave', {
id: "101",
minionCapacity: 5000,
vicinity: "California, USA",
infiltrated: true
}),
homePlanet: env.store.createRecord('home-planet', {
id: "123",
name: "Villain League"
})
}
);
});

run(function() {
json = tom.serialize();
});

deepEqual(json, {
firstName: get(tom, "firstName"),
lastName: get(tom, "lastName"),
homePlanet: get(tom, "homePlanet").get("id"),
secretLabType: 'batCave',
secretLab: {
id: get(tom, "secretLab").get("id"),
minionCapacity: get(tom, "secretLab").get("minionCapacity"),
vicinity: get(tom, "secretLab").get("vicinity"),
infiltrated: true
}
});
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure if this is the way this should work. Now that I think about it, this probably should be a type property inside the secretLab object instead of the secretLabType, right?

Copy link
Member

Choose a reason for hiding this comment

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

I'm also not sure about the correct behavior here. cc @igorT @cyril-sf

});

test("serialize with embedded object (belongsTo relationship) works with different primaryKeys", function() {
env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
primaryKey: '_id',
Expand Down Expand Up @@ -1236,6 +1287,111 @@ test("serialize with embedded object (belongsTo relationship, new no id)", funct
});
});

test("serialize with embedded object (polymorphic belongsTo relationship) supports serialize:ids", function() {
SuperVillain.reopen({
secretLab: DS.belongsTo('secret-lab', { polymorphic: true })
});

env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { serialize: 'ids' }
}
}));

var tom, json;
run(function() {
tom = env.store.createRecord(
'super-villain',
{ firstName: "Tom", lastName: "Dale", id: "1",
secretLab: env.store.createRecord('bat-cave', { minionCapacity: 5000, vicinity: "California, USA", id: "101" }),
homePlanet: env.store.createRecord('home-planet', { name: "Villain League", id: "123" })
}
);
});

run(function() {
json = tom.serialize();
});

deepEqual(json, {
firstName: get(tom, "firstName"),
lastName: get(tom, "lastName"),
homePlanet: get(tom, "homePlanet").get("id"),
secretLab: get(tom, "secretLab").get("id"),
secretLabType: 'batCave'
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:id", function() {
SuperVillain.reopen({
secretLab: DS.belongsTo('secret-lab', { polymorphic: true })
});

env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { serialize: 'id' }
}
}));

var tom, json;
run(function() {
tom = env.store.createRecord(
'super-villain',
{ firstName: "Tom", lastName: "Dale", id: "1",
secretLab: env.store.createRecord('bat-cave', { minionCapacity: 5000, vicinity: "California, USA", id: "101" }),
homePlanet: env.store.createRecord('home-planet', { name: "Villain League", id: "123" })
}
);
});

run(function() {
json = tom.serialize();
});

deepEqual(json, {
firstName: get(tom, "firstName"),
lastName: get(tom, "lastName"),
homePlanet: get(tom, "homePlanet").get("id"),
secretLab: get(tom, "secretLab").get("id"),
secretLabType: 'batCave'
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:id in conjunction with deserialize:records", function() {
SuperVillain.reopen({
secretLab: DS.belongsTo('secret-lab', { polymorphic: true })
});

env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { serialize: 'id', deserialize: 'records' }
}
}));

var tom, json;
run(function() {
tom = env.store.createRecord(
'super-villain',
{ firstName: "Tom", lastName: "Dale", id: "1",
secretLab: env.store.createRecord('bat-cave', { minionCapacity: 5000, vicinity: "California, USA", id: "101" }),
homePlanet: env.store.createRecord('home-planet', { name: "Villain League", id: "123" })
}
);
});

run(function() {
json = tom.serialize();
});

deepEqual(json, {
firstName: get(tom, "firstName"),
lastName: get(tom, "lastName"),
homePlanet: get(tom, "homePlanet").get("id"),
secretLab: get(tom, "secretLab").get("id"),
secretLabType: 'batCave'
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:ids", function() {
env.registry.register('serializer:super-villain', DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
Expand Down