Skip to content

Commit

Permalink
Make sure ActiveResource::Errors#from_json doesn't pass nil to #from_…
Browse files Browse the repository at this point in the history
…array [#3650 state:commited]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
David Trasbo authored and josevalim committed Jun 26, 2010
1 parent 7eb5766 commit df083b4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion activeresource/lib/active_resource/validations.rb
Expand Up @@ -27,7 +27,7 @@ def from_array(messages, save_cache = false)

# 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

Expand Down
13 changes: 12 additions & 1 deletion activeresource/test/cases/base_errors_test.rb
Expand Up @@ -17,7 +17,7 @@ def test_should_mark_as_invalid
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
Expand All @@ -26,6 +26,17 @@ def test_should_parse_xml_errors
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
Expand Down

0 comments on commit df083b4

Please sign in to comment.