Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions lib/active_resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,6 @@ module ActiveResource
# ryan.errors.invalid?(:first) # => true
# ryan.errors.full_messages # => ['First cannot be empty']
#
# For backwards-compatibility with older endpoints, the following formats are also supported in JSON responses:
#
# # {"errors":['First cannot be empty']}
# # This was the required format for previous versions of ActiveResource
# # {"first":["cannot be empty"]}
# # This was the default format produced by respond_with in ActionController <3.2.1
#
# Parsing either of these formats will result in a deprecation warning.
#
# Learn more about Active Resource's validation features in the ActiveResource::Validations documentation.
#
# === Timeouts
Expand Down
17 changes: 2 additions & 15 deletions lib/active_resource/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,8 @@ def from_hash(messages, save_cache = false)
# Grabs errors from a json response.
def from_json(json, save_cache = false)
decoded = ActiveSupport::JSON.decode(json) || {} rescue {}
if decoded.kind_of?(Hash) && (decoded.has_key?("errors") || decoded.empty?)
errors = decoded["errors"] || {}
if errors.kind_of?(Array)
# 3.2.1-style with array of strings
ActiveResource.deprecator.warn("Returning errors as an array of strings is deprecated.")
from_array errors, save_cache
else
# 3.2.2+ style
from_hash errors, save_cache
end
else
# <3.2-style respond_with - lacks 'errors' key
ActiveResource.deprecator.warn('Returning errors as a hash without a root "errors" key is deprecated.')
from_hash decoded, save_cache
end
errors = decoded["errors"] || {}
from_hash errors, save_cache
end

# Grabs errors from an XML response.
Expand Down
34 changes: 0 additions & 34 deletions test/cases/base_errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,40 +111,6 @@ def test_should_mark_as_invalid_when_content_type_is_unavailable_in_response_hea
end
end

def test_should_parse_json_string_errors_with_an_errors_key
ActiveResource::HttpMock.respond_to do |mock|
mock.post "/people.json", {}, %q({"errors":["Age can't be blank", "Name can't be blank", "Name must start with a letter", "Person quota full for today.", "Phone work can't be blank", "Phone is not valid"]}), 422, "Content-Type" => "application/json; charset=utf-8"
end

assert_deprecated(/as an array/, ActiveResource.deprecator) do
invalid_user_using_format(:json) do
assert @person.errors[:name].any?
assert_equal [ "can't be blank" ], @person.errors[:age]
assert_equal [ "can't be blank", "must start with a letter" ], @person.errors[:name]
assert_equal [ "is not valid" ], @person.errors[:phone]
assert_equal [ "can't be blank" ], @person.errors[:phone_work]
assert_equal [ "Person quota full for today." ], @person.errors[:base]
end
end
end

def test_should_parse_3_1_style_json_errors
ActiveResource::HttpMock.respond_to do |mock|
mock.post "/people.json", {}, %q({"age":["can't be blank"],"name":["can't be blank", "must start with a letter"],"person":["quota full for today."],"phone_work":["can't be blank"],"phone":["is not valid"]}), 422, "Content-Type" => "application/json; charset=utf-8"
end

assert_deprecated(/without a root/, ActiveResource.deprecator) do
invalid_user_using_format(:json) do
assert @person.errors[:name].any?
assert_equal [ "can't be blank" ], @person.errors[:age]
assert_equal [ "can't be blank", "must start with a letter" ], @person.errors[:name]
assert_equal [ "is not valid" ], @person.errors[:phone]
assert_equal [ "can't be blank" ], @person.errors[:phone_work]
assert_equal [ "Person quota full for today." ], @person.errors[:base]
end
end
end

private
def invalid_user_using_format(mime_type_reference)
previous_format = Person.format
Expand Down