Skip to content

Commit 07e289b

Browse files
committed
CSHARP-5792: Restore previous RepresentationConverter behavior for decimal.MaxValue and decimal.MinValue. Fix Decimal128.ToDecimal conversion and add checks for Min and MaxValue.
1 parent 3d78530 commit 07e289b

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

src/MongoDB.Bson/ObjectModel/Decimal128.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,17 @@ public struct Decimal128 : IConvertible, IComparable<Decimal128>, IEquatable<Dec
4242

4343
// public static properties
4444
/// <summary>
45-
/// Gets the maximum Decimal128 value.
45+
/// Gets the maximum value.
4646
/// </summary>
4747
public static Decimal128 MaxValue =>
4848
__maxValue;
4949

5050
/// <summary>
51-
/// Gets the minimum Decimal128 value.
51+
/// Gets the minimum value.
5252
/// </summary>
5353
public static Decimal128 MinValue =>
5454
__minValue;
5555

56-
/// <summary>
57-
/// Gets the maximum System.Decimal value.
58-
/// </summary>
59-
public static Decimal128 MaxDecimalValue =>
60-
__maxDecimalValue;
61-
62-
/// <summary>
63-
/// Gets the minimum System.Decimal value.
64-
/// </summary>
65-
public static Decimal128 MinDecimalValue =>
66-
__minDecimalValue;
67-
6856
/// <summary>
6957
/// Represents negative infinity.
7058
/// </summary>
@@ -751,6 +739,14 @@ public static byte ToByte(Decimal128 d)
751739
/// <returns>A <see cref="decimal"/> equivalent to <paramref name="d" />.</returns>
752740
public static decimal ToDecimal(Decimal128 d)
753741
{
742+
if (d == Decimal128.MaxValue)
743+
{
744+
return decimal.MaxValue;
745+
}
746+
else if (d == Decimal128.MinValue)
747+
{
748+
return decimal.MinValue;
749+
}
754750
if (Flags.IsFirstForm(d._highBits))
755751
{
756752
if (Decimal128.Compare(d, __minDecimalValue) < 0 || Decimal128.Compare(d, __maxDecimalValue) > 0)

src/MongoDB.Bson/Serialization/Options/RepresentationConverter.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ obj is RepresentationConverter other &&
7878
/// <returns>A Decimal.</returns>
7979
public decimal ToDecimal(Decimal128 value)
8080
{
81-
// comparison against Decimal128.MaxValue remains valid for backwards compat.
82-
if (value == Decimal128.MaxValue || value == Decimal128.MaxDecimalValue)
81+
if (value == Decimal128.MaxValue)
8382
{
8483
return decimal.MaxValue;
8584
}
86-
// comparison against Decimal128.MinValue remains valid for backwards compat.
87-
else if (value == Decimal128.MinValue || value == Decimal128.MinDecimalValue)
85+
else if (value == Decimal128.MinValue)
8886
{
8987
return decimal.MinValue;
9088
}
@@ -168,11 +166,11 @@ public Decimal128 ToDecimal128(decimal value)
168166
{
169167
if (value == decimal.MaxValue)
170168
{
171-
return Decimal128.MaxDecimalValue;
169+
return Decimal128.MaxValue;
172170
}
173171
else if (value == decimal.MinValue)
174172
{
175-
return Decimal128.MinDecimalValue;
173+
return Decimal128.MinValue;
176174
}
177175

178176
// conversion from decimal to Decimal128 is lossless so need to check for overflow or truncation

tests/MongoDB.Bson.Tests/ObjectModel/Decimal128Tests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public void Decimal(string valueString, string expectedResult)
7676
[InlineData("1E-99", "0")]
7777
[InlineData("1E-6111", "0")]
7878
[InlineData("10000.0000000000000000000000001", "10000.000000000000000000000000")] // see: CSHARP-2001
79+
[InlineData("9999999999999999999999999999999999E+6111", "79228162514264337593543950335")] // see: CSHARP-5792
80+
[InlineData("-9999999999999999999999999999999999E+6111", "-79228162514264337593543950335")]
7981
public void ToDecimal_should_return_expected_result(string valueString, string expectedResultString)
8082
{
8183
var subject = Decimal128.Parse(valueString);

tests/MongoDB.Bson.Tests/Serialization/Options/RepresentationConverterTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,9 @@ public void TestConversions()
123123
// general decimal <-> Decimal128 checks
124124
Assert.Equal(123.45m, converter.ToDecimal(new Decimal128(123.45m)));
125125
Assert.Equal(-123.45m, converter.ToDecimal(new Decimal128(-123.45m)));
126-
Assert.Equal(Decimal128.MaxDecimalValue, converter.ToDecimal128(decimal.MaxValue));
127-
Assert.Equal(Decimal128.MinDecimalValue, converter.ToDecimal128(decimal.MinValue));
128-
Assert.Equal(decimal.MaxValue, converter.ToDecimal(Decimal128.MaxDecimalValue));
129-
Assert.Equal(decimal.MinValue, converter.ToDecimal(Decimal128.MinDecimalValue));
126+
// System.Decimal should be mapped to Decimal128.MaxValue and vice versa
127+
Assert.Equal(Decimal128.MaxValue, converter.ToDecimal128(decimal.MaxValue));
128+
Assert.Equal(Decimal128.MinValue, converter.ToDecimal128(decimal.MinValue));
130129
Assert.Equal(decimal.MaxValue, converter.ToDecimal(Decimal128.MaxValue));
131130
Assert.Equal(decimal.MinValue, converter.ToDecimal(Decimal128.MinValue));
132131
}

0 commit comments

Comments
 (0)