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

Treat empty strings in ids the same as null or undefined #3253

Merged
merged 1 commit into from Jun 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ember-data/lib/system/coerce-id.js
Expand Up @@ -5,5 +5,5 @@
// ID into the URL, and if we later try to deserialize that URL and find the
// corresponding record, we will not know if it is a string or a number.
export default function coerceId(id) {
return id == null ? null : id+'';
return id == null || id === '' ? null : id+'';
}
41 changes: 39 additions & 2 deletions packages/ember-data/tests/integration/client-id-generation-test.js
@@ -1,5 +1,5 @@
var get = Ember.get;
var Post, Comment, env;
var Post, Comment, Misc, env;
var run = Ember.run;

module("integration/client_id_generation - Client-side ID Generation", {
Expand All @@ -12,9 +12,14 @@ module("integration/client_id_generation - Client-side ID Generation", {
comments: DS.hasMany('comment')
});

Misc = DS.Model.extend({
foo: DS.attr('string')
});

env = setupStore({
post: Post,
comment: Comment
comment: Comment,
misc: Misc
});
},

Expand Down Expand Up @@ -60,3 +65,35 @@ test("If an adapter implements the `generateIdForRecord` method, the store shoul
post.save();
});
});

test("empty string and undefined ids should coerce to null", function() {
expect(6);
var comment, post;
var idCount = 0;
var ids = [undefined, ''];
env.adapter.generateIdForRecord = function(passedStore, record) {
equal(env.store, passedStore, "store is the first parameter");

return ids[idCount++];
};

env.adapter.createRecord = function(store, type, record) {
equal(typeof get(record, 'id'), 'object', 'correct type');
return Ember.RSVP.resolve();
};

run(function() {
comment = env.store.createRecord('misc');
post = env.store.createRecord('misc');
});

equal(get(comment, 'id'), null, "comment is assigned id 'null'");
equal(get(post, 'id'), null, "post is assigned id 'null'");

// Despite client-generated IDs, calling commit() on the store should still
// invoke the adapter's `createRecord` method.
run(function() {
comment.save();
post.save();
});
});