Skip to content

Commit dd504b6

Browse files
committed
Remove Validations deprecation code
The `ActiveResource::Validations#from_json` method emits deprecation warnings when handling error response payloads that resemble the following: ``` {"errors":["First cannot be empty"]} {"first":["cannot be empty"]} ``` The deprecation was originally introduced in [c643605][] (Feb 4, 2012). This commit removes documentation mentions of backwards-compatibility along with the conditional branches that emit the deprecation warnings. The removal of the backwards support is a breaking change. [c643605]: c643605
1 parent bd154d2 commit dd504b6

File tree

3 files changed

+2
-58
lines changed

3 files changed

+2
-58
lines changed

lib/active_resource/base.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,6 @@ module ActiveResource
325325
# ryan.errors.invalid?(:first) # => true
326326
# ryan.errors.full_messages # => ['First cannot be empty']
327327
#
328-
# For backwards-compatibility with older endpoints, the following formats are also supported in JSON responses:
329-
#
330-
# # {"errors":['First cannot be empty']}
331-
# # This was the required format for previous versions of ActiveResource
332-
# # {"first":["cannot be empty"]}
333-
# # This was the default format produced by respond_with in ActionController <3.2.1
334-
#
335-
# Parsing either of these formats will result in a deprecation warning.
336-
#
337328
# Learn more about Active Resource's validation features in the ActiveResource::Validations documentation.
338329
#
339330
# === Timeouts

lib/active_resource/validations.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,8 @@ def from_hash(messages, save_cache = false)
5353
# Grabs errors from a json response.
5454
def from_json(json, save_cache = false)
5555
decoded = ActiveSupport::JSON.decode(json) || {} rescue {}
56-
if decoded.kind_of?(Hash) && (decoded.has_key?("errors") || decoded.empty?)
57-
errors = decoded["errors"] || {}
58-
if errors.kind_of?(Array)
59-
# 3.2.1-style with array of strings
60-
ActiveResource.deprecator.warn("Returning errors as an array of strings is deprecated.")
61-
from_array errors, save_cache
62-
else
63-
# 3.2.2+ style
64-
from_hash errors, save_cache
65-
end
66-
else
67-
# <3.2-style respond_with - lacks 'errors' key
68-
ActiveResource.deprecator.warn('Returning errors as a hash without a root "errors" key is deprecated.')
69-
from_hash decoded, save_cache
70-
end
56+
errors = decoded["errors"] || {}
57+
from_hash errors, save_cache
7158
end
7259

7360
# Grabs errors from an XML response.

test/cases/base_errors_test.rb

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -111,40 +111,6 @@ def test_should_mark_as_invalid_when_content_type_is_unavailable_in_response_hea
111111
end
112112
end
113113

114-
def test_should_parse_json_string_errors_with_an_errors_key
115-
ActiveResource::HttpMock.respond_to do |mock|
116-
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"
117-
end
118-
119-
assert_deprecated(/as an array/, ActiveResource.deprecator) do
120-
invalid_user_using_format(:json) do
121-
assert @person.errors[:name].any?
122-
assert_equal [ "can't be blank" ], @person.errors[:age]
123-
assert_equal [ "can't be blank", "must start with a letter" ], @person.errors[:name]
124-
assert_equal [ "is not valid" ], @person.errors[:phone]
125-
assert_equal [ "can't be blank" ], @person.errors[:phone_work]
126-
assert_equal [ "Person quota full for today." ], @person.errors[:base]
127-
end
128-
end
129-
end
130-
131-
def test_should_parse_3_1_style_json_errors
132-
ActiveResource::HttpMock.respond_to do |mock|
133-
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"
134-
end
135-
136-
assert_deprecated(/without a root/, ActiveResource.deprecator) do
137-
invalid_user_using_format(:json) do
138-
assert @person.errors[:name].any?
139-
assert_equal [ "can't be blank" ], @person.errors[:age]
140-
assert_equal [ "can't be blank", "must start with a letter" ], @person.errors[:name]
141-
assert_equal [ "is not valid" ], @person.errors[:phone]
142-
assert_equal [ "can't be blank" ], @person.errors[:phone_work]
143-
assert_equal [ "Person quota full for today." ], @person.errors[:base]
144-
end
145-
end
146-
end
147-
148114
private
149115
def invalid_user_using_format(mime_type_reference)
150116
previous_format = Person.format

0 commit comments

Comments
 (0)