Skip to content

Commit 1fb7969

Browse files
committed
Raise an error when AS::JSON.decode is called with options
Rails 4.1 has switched away from MultiJson, and does not currently support any options on `ActiveSupport::JSON.decode`. Passing in unsupported options (i.e. any non-empty options hash) will now raise an ArgumentError. Rationale: 1. We cannot guarantee the underlying JSON parser won't change in the future, hence we cannot guarantee a consistent set of options the method could take 2. The `json` gem, which happens to be the current JSON parser, takes many dangerous options that is irrelevant to the purpose of AS's JSON decoding API 3. To reserve the options hash for future use, e.g. overriding default global options like ActiveSupport.parse_json_times This change *DOES NOT* introduce any changes in the public API. The signature of the method is still decode(json_text, options). The difference is this method previously accepted undocumented options which does different things when the underlying adapter changes. It now correctly raises an ArgumentError when it encounters options that it does not recognize (and currently it does not support any options).
1 parent dae66a0 commit 1fb7969

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Calling ActiveSupport::JSON.decode with unsupported options now raises an error.
2+
3+
*Godfrey Chan*
4+
15
* Support :unless_exist in FileStore
26

37
*Michael Grosser*

activesupport/lib/active_support/json/decoding.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ class << self
1414
# ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}")
1515
# => {"team" => "rails", "players" => "36"}
1616
def decode(json, options = {})
17-
data = ::JSON.parse(json, options.merge(create_additions: false, quirks_mode: true))
17+
if options.present?
18+
raise ArgumentError, "In Rails 4.1, ActiveSupport::JSON.decode no longer " \
19+
"accepts an options hash for MultiJSON. MultiJSON reached its end of life " \
20+
"and has been removed."
21+
end
22+
23+
data = ::JSON.parse(json, quirks_mode: true)
24+
1825
if ActiveSupport.parse_json_times
1926
convert_dates_from(data)
2027
else

activesupport/test/json/decoding_test.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,8 @@ def test_failed_json_decoding
9898
assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%()) }
9999
end
100100

101-
def test_cannot_force_json_unmarshalling
102-
encodeded = %q({"json_class":"TestJSONDecoding::Foo"})
103-
decodeded = {"json_class"=>"TestJSONDecoding::Foo"}
104-
assert_equal decodeded, ActiveSupport::JSON.decode(encodeded, create_additions: true)
101+
def test_cannot_pass_unsupported_options
102+
assert_raise(ArgumentError) { ActiveSupport::JSON.decode("", create_additions: true) }
105103
end
106104
end
107105

0 commit comments

Comments
 (0)