Skip to content

Commit a60ccad

Browse files
committed
Merge pull request rails#13911 from davidcelis/remove-bigdecimal-serialization
Deprecate custom BigDecimal serialization Conflicts: activesupport/CHANGELOG.md
2 parents 682a579 + c87b27e commit a60ccad

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed

activesupport/CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
* Deprecate custom `BigDecimal` serialization
2+
3+
Deprecate the custom `BigDecimal` serialization that is included when requiring
4+
`active_support/all` as a fix for #12467. Let Ruby handle YAML serialization
5+
for `BigDecimal` instead.
6+
7+
*David Celis*
8+
19
* Fix parsing bugs in `XmlMini`
210

3-
Symbols or boolean parsing would raise an error for non string values (e.g.
11+
Symbols or boolean parsing would raise an error for non string values (e.g.
412
integers). Decimal parsing would fail due to a missing requirement.
513

614
*Birkir A. Barkarson*

activesupport/lib/active_support/core_ext/big_decimal/conversions.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
require 'bigdecimal'
22
require 'bigdecimal/util'
3-
require 'yaml'
43

54
class BigDecimal
6-
YAML_MAPPING = { 'Infinity' => '.Inf', '-Infinity' => '-.Inf', 'NaN' => '.NaN' }
7-
8-
def encode_with(coder)
9-
string = to_s
10-
coder.represent_scalar(nil, YAML_MAPPING[string] || string)
11-
end
12-
13-
# Backport this method if it doesn't exist
14-
unless method_defined?(:to_d)
15-
def to_d
16-
self
17-
end
18-
end
19-
205
DEFAULT_STRING_FORMAT = 'F'
216
def to_formatted_s(*args)
227
if args[0].is_a?(Symbol)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
ActiveSupport::Deprecation.warn 'core_ext/big_decimal/yaml_conversions is deprecated and will be removed in the future.'
2+
3+
require 'bigdecimal'
4+
require 'yaml'
5+
6+
class BigDecimal
7+
YAML_MAPPING = { 'Infinity' => '.Inf', '-Infinity' => '-.Inf', 'NaN' => '.NaN' }
8+
9+
def encode_with(coder)
10+
string = to_s
11+
coder.represent_scalar(nil, YAML_MAPPING[string] || string)
12+
end
13+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'abstract_unit'
2+
require 'active_support/core_ext/big_decimal/yaml_conversions'
3+
4+
class BigDecimalYamlConversionsTest < ActiveSupport::TestCase
5+
def test_to_yaml
6+
assert_match("--- 100000.30020320320000000000000000000000000000001\n", BigDecimal.new('100000.30020320320000000000000000000000000000001').to_yaml)
7+
assert_match("--- .Inf\n", BigDecimal.new('Infinity').to_yaml)
8+
assert_match("--- .NaN\n", BigDecimal.new('NaN').to_yaml)
9+
assert_match("--- -.Inf\n", BigDecimal.new('-Infinity').to_yaml)
10+
end
11+
end

activesupport/test/core_ext/bigdecimal_test.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@
22
require 'active_support/core_ext/big_decimal'
33

44
class BigDecimalTest < ActiveSupport::TestCase
5-
def test_to_yaml
6-
assert_match("--- 100000.30020320320000000000000000000000000000001\n", BigDecimal.new('100000.30020320320000000000000000000000000000001').to_yaml)
7-
assert_match("--- .Inf\n", BigDecimal.new('Infinity').to_yaml)
8-
assert_match("--- .NaN\n", BigDecimal.new('NaN').to_yaml)
9-
assert_match("--- -.Inf\n", BigDecimal.new('-Infinity').to_yaml)
10-
end
11-
12-
def test_to_d
13-
bd = BigDecimal.new '10'
14-
assert_equal bd, bd.to_d
15-
end
16-
175
def test_to_s
186
bd = BigDecimal.new '0.01'
197
assert_equal '0.01', bd.to_s

0 commit comments

Comments
 (0)