diff --git a/app/assets/javascripts/discourse/models/store.js.es6 b/app/assets/javascripts/discourse/models/store.js.es6 index af365c7ba..39e08f1cb 100644 --- a/app/assets/javascripts/discourse/models/store.js.es6 +++ b/app/assets/javascripts/discourse/models/store.js.es6 @@ -268,7 +268,9 @@ export default Ember.Object.extend({ _hydrate(type, obj, root) { if (!obj) { throw "Can't hydrate " + type + " of `null`"; } - if (!obj.id) { throw "Can't hydrate " + type + " without an `id`"; } + + const id = obj.id; + if (!id) { throw "Can't hydrate " + type + " without an `id`"; } root = root || obj; @@ -278,13 +280,14 @@ export default Ember.Object.extend({ this._hydrateEmbedded(type, obj, root); } - const existing = fromMap(type, obj.id); + const existing = fromMap(type, id); if (existing === obj) { return existing; } if (existing) { delete obj.id; const klass = this.container.lookupFactory('model:' + type) || RestModel; existing.setProperties(klass.munge(obj)); + obj.id = id; return existing; } diff --git a/test/javascripts/models/store-test.js.es6 b/test/javascripts/models/store-test.js.es6 index 4c72c2ae7..e54f5740b 100644 --- a/test/javascripts/models/store-test.js.es6 +++ b/test/javascripts/models/store-test.js.es6 @@ -19,6 +19,18 @@ test('createRecord without an `id`', function() { ok(!widget.get('id'), 'there is no id'); }); +test("createRecord doesn't modify the input `id` field", () => { + const store = createStore(); + const widget = store.createRecord('widget', {id: 1, name: 'hello'}); + + const obj = { id: 1, name: 'something' }; + + const other = store.createRecord('widget', obj); + equal(widget, other, 'returns the same record'); + equal(widget.name, 'something', 'it updates the properties'); + equal(obj.id, 1, 'it does not remove the id from the input'); +}); + test('createRecord without attributes', function() { const store = createStore(); const widget = store.createRecord('widget');