From 375e980b046bcc25bc7604aa9e99043e1761e917 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Sun, 17 May 2015 19:54:26 -1000 Subject: [PATCH 1/2] Pass through the parsed response directly again The changes in e928355f doesn't make the behaviour as backwards compatible as we would like. The changes would break existing systems that do depend on a snake case errors hash as we've seen in #3067. For now restore the original behaviour so that we can make a release. We should make a decision at some point whether to use the documented interface for `InvalidError` or to change the interface. Part of the issue here is that we don't have an integration test to qualify that the errors hash gets read correctly and camelized. There's been some confusion as to whether or not ember data actually does this. --- .../lib/system/active-model-adapter.js | 4 +--- .../tests/integration/active-model-adapter-test.js | 14 +------------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/packages/activemodel-adapter/lib/system/active-model-adapter.js b/packages/activemodel-adapter/lib/system/active-model-adapter.js index 7a39ca4e820..39e8a2c8df6 100644 --- a/packages/activemodel-adapter/lib/system/active-model-adapter.js +++ b/packages/activemodel-adapter/lib/system/active-model-adapter.js @@ -142,9 +142,7 @@ var ActiveModelAdapter = RESTAdapter.extend({ if (jqXHR && jqXHR.status === 422) { var response = Ember.$.parseJSON(jqXHR.responseText); - var errors = response.errors ? response.errors : response; - - return new InvalidError(errors); + return new InvalidError(response); } else { return error; } diff --git a/packages/activemodel-adapter/tests/integration/active-model-adapter-test.js b/packages/activemodel-adapter/tests/integration/active-model-adapter-test.js index 8e8984fa99b..d4f57b18e67 100644 --- a/packages/activemodel-adapter/tests/integration/active-model-adapter-test.js +++ b/packages/activemodel-adapter/tests/integration/active-model-adapter-test.js @@ -21,25 +21,13 @@ test('buildURL - decamelizes names', function() { }); test('ajaxError - returns invalid error if 422 response', function() { - var error = new DS.InvalidError({ name: "can't be blank" }); - - var jqXHR = { - status: 422, - responseText: JSON.stringify({ errors: { name: "can't be blank" } }) - }; - - equal(adapter.ajaxError(jqXHR), error.toString()); -}); - -test('ajaxError - accepts any kind of json on 422', function() { - var error = new DS.InvalidError({ name: "can't be blank" }); var jqXHR = { status: 422, responseText: JSON.stringify({ name: "can't be blank" }) }; - equal(adapter.ajaxError(jqXHR), error.toString()); + equal(adapter.ajaxError(jqXHR).errors.name, "can't be blank"); }); test('ajaxError - returns ajax response if not 422 response', function() { From 17a2a18e5c7cf0b52059ef70a28dccfd66de6c1b Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Sun, 17 May 2015 20:22:53 -1000 Subject: [PATCH 2/2] Test that active model adapter passes errors to serializer Thanks to @pangratz for putting this test together. Currently we only have tests for the adapter and serializer correctly behaving in isolation. --- .../active-model-adapter-serializer-test.js | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 packages/activemodel-adapter/tests/integration/active-model-adapter-serializer-test.js diff --git a/packages/activemodel-adapter/tests/integration/active-model-adapter-serializer-test.js b/packages/activemodel-adapter/tests/integration/active-model-adapter-serializer-test.js new file mode 100644 index 00000000000..6cfbbc58b2b --- /dev/null +++ b/packages/activemodel-adapter/tests/integration/active-model-adapter-serializer-test.js @@ -0,0 +1,54 @@ +var env, store, adapter, User; +var originalAjax; + +module("integration/active_model_adapter_serializer - AMS Adapter and Serializer", { + setup: function() { + originalAjax = Ember.$.ajax; + + User = DS.Model.extend({ + firstName: DS.attr() + }); + + env = setupStore({ + user: User, + adapter: DS.ActiveModelAdapter + }); + + store = env.store; + adapter = env.adapter; + + env.registry.register('serializer:application', DS.ActiveModelSerializer); + }, + + teardown: function() { + Ember.$.ajax = originalAjax; + } +}); + +test('errors are camelCased and are expected under the `errors` property of the payload', function() { + var jqXHR = { + status: 422, + responseText: JSON.stringify({ + errors: { + first_name: ["firstName error"] + } + }) + }; + + Ember.$.ajax = function(hash) { + hash.error(jqXHR); + }; + + var user; + Ember.run(function() { + user = store.push('user', { id: 1 }); + }); + + Ember.run(function() { + user.save().then(null, function() { + var errors = user.get('errors'); + ok(errors.has('firstName'), "there are errors for the firstName attribute"); + deepEqual(errors.errorsFor('firstName').getEach('message'), ['firstName error']); + }); + }); +});