Skip to content

Commit 3d78530

Browse files
committed
#CSHARP-5792: Fix Decimal.MaxValue and Decimal.MinValue serialization behavior in RepresentationConverter, add decimal test cases to RepresentationConverterTests
1 parent cf91a58 commit 3d78530

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/MongoDB.Bson/ObjectModel/Decimal128.cs

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

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

5050
/// <summary>
51-
/// Gets the minimum value.
51+
/// Gets the minimum Decimal128 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+
5668
/// <summary>
5769
/// Represents negative infinity.
5870
/// </summary>

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ obj is RepresentationConverter other &&
7878
/// <returns>A Decimal.</returns>
7979
public decimal ToDecimal(Decimal128 value)
8080
{
81-
if (value == Decimal128.MaxValue)
81+
// comparison against Decimal128.MaxValue remains valid for backwards compat.
82+
if (value == Decimal128.MaxValue || value == Decimal128.MaxDecimalValue)
8283
{
8384
return decimal.MaxValue;
8485
}
85-
else if (value == Decimal128.MinValue)
86+
// comparison against Decimal128.MinValue remains valid for backwards compat.
87+
else if (value == Decimal128.MinValue || value == Decimal128.MinDecimalValue)
8688
{
8789
return decimal.MinValue;
8890
}
@@ -166,11 +168,11 @@ public Decimal128 ToDecimal128(decimal value)
166168
{
167169
if (value == decimal.MaxValue)
168170
{
169-
return Decimal128.MaxValue;
171+
return Decimal128.MaxDecimalValue;
170172
}
171173
else if (value == decimal.MinValue)
172174
{
173-
return Decimal128.MinValue;
175+
return Decimal128.MinDecimalValue;
174176
}
175177

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ public void TestConversions()
119119
Assert.Equal((long)(ulong)int.MaxValue, converter.ToInt64(int.MaxValue));
120120
Assert.Equal(1UL, converter.ToUInt64((long)1));
121121
Assert.Equal((long)(ulong)long.MaxValue, converter.ToInt64(long.MaxValue));
122+
123+
// general decimal <-> Decimal128 checks
124+
Assert.Equal(123.45m, converter.ToDecimal(new Decimal128(123.45m)));
125+
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));
130+
Assert.Equal(decimal.MaxValue, converter.ToDecimal(Decimal128.MaxValue));
131+
Assert.Equal(decimal.MinValue, converter.ToDecimal(Decimal128.MinValue));
122132
}
123133

124134
[Theory]

0 commit comments

Comments
 (0)