Skip to content

Commit 1698789

Browse files
committed
move max and min value checks into IsFirstForm check, add blank line above comment in test
1 parent 07e289b commit 1698789

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/MongoDB.Bson/ObjectModel/Decimal128.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,16 +739,18 @@ public static byte ToByte(Decimal128 d)
739739
/// <returns>A <see cref="decimal"/> equivalent to <paramref name="d" />.</returns>
740740
public static decimal ToDecimal(Decimal128 d)
741741
{
742-
if (d == Decimal128.MaxValue)
743-
{
744-
return decimal.MaxValue;
745-
}
746-
else if (d == Decimal128.MinValue)
747-
{
748-
return decimal.MinValue;
749-
}
750742
if (Flags.IsFirstForm(d._highBits))
751743
{
744+
// fix for CSHARP-5792, make sure that Decimal128.MaxValue and MinValue are mapped back to
745+
// decimal.MaxValue and MinValue.
746+
if (d == Decimal128.MaxValue)
747+
{
748+
return decimal.MaxValue;
749+
}
750+
if (d == Decimal128.MinValue)
751+
{
752+
return decimal.MinValue;
753+
}
752754
if (Decimal128.Compare(d, __minDecimalValue) < 0 || Decimal128.Compare(d, __maxDecimalValue) > 0)
753755
{
754756
throw new OverflowException("Value is too large or too small to be converted to a Decimal.");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ 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+
126127
// System.Decimal should be mapped to Decimal128.MaxValue and vice versa
127128
Assert.Equal(Decimal128.MaxValue, converter.ToDecimal128(decimal.MaxValue));
128129
Assert.Equal(Decimal128.MinValue, converter.ToDecimal128(decimal.MinValue));

0 commit comments

Comments
 (0)