aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activeresource/lib/active_resource/validations.rb2
-rw-r--r--activeresource/test/cases/base_errors_test.rb13
2 files changed, 13 insertions, 2 deletions
diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb
index a19e0d0ac9..026d81e44a 100644
--- a/activeresource/lib/active_resource/validations.rb
+++ b/activeresource/lib/active_resource/validations.rb
@@ -27,7 +27,7 @@ module ActiveResource
# Grabs errors from a json response.
def from_json(json, save_cache = false)
- array = ActiveSupport::JSON.decode(json)['errors'] rescue []
+ array = Array.wrap(ActiveSupport::JSON.decode(json)['errors']) rescue []
from_array array, save_cache
end
diff --git a/activeresource/test/cases/base_errors_test.rb b/activeresource/test/cases/base_errors_test.rb
index b4fd75fba3..5063916d10 100644
--- a/activeresource/test/cases/base_errors_test.rb
+++ b/activeresource/test/cases/base_errors_test.rb
@@ -17,7 +17,7 @@ class BaseErrorsTest < Test::Unit::TestCase
end
end
- def test_should_parse_xml_errors
+ def test_should_parse_json_and_xml_errors
[ :json, :xml ].each do |format|
invalid_user_using_format(format) do
assert_kind_of ActiveResource::Errors, @person.errors
@@ -26,6 +26,17 @@ class BaseErrorsTest < Test::Unit::TestCase
end
end
+ def test_should_parse_json_errors_when_no_errors_key
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.post "/people.json", {}, '{}', 422, {'Content-Type' => 'application/json; charset=utf-8'}
+ end
+
+ invalid_user_using_format(:json) do
+ assert_kind_of ActiveResource::Errors, @person.errors
+ assert_equal 0, @person.errors.size
+ end
+ end
+
def test_should_parse_errors_to_individual_attributes
[ :json, :xml ].each do |format|
invalid_user_using_format(format) do