Skip to content

Commit 536fd85

Browse files
committed
CSHARP-4993: Avoid TruncationException when deserializing Decimal from Decimal128.
1 parent b0fbe1d commit 536fd85

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

src/MongoDB.Bson/Serialization/Attributes/BsonRepresentationAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace MongoDB.Bson.Serialization.Attributes
2525
public class BsonRepresentationAttribute : BsonSerializationOptionsAttribute
2626
{
2727
// private fields
28-
private BsonType _representation;
28+
private readonly BsonType _representation;
2929
private bool _allowOverflow;
3030
private bool _allowTruncation;
3131

@@ -37,6 +37,8 @@ public class BsonRepresentationAttribute : BsonSerializationOptionsAttribute
3737
public BsonRepresentationAttribute(BsonType representation)
3838
{
3939
_representation = representation;
40+
_allowOverflow = false;
41+
_allowTruncation = representation == BsonType.Decimal128;
4042
}
4143

4244
// public properties

src/MongoDB.Bson/Serialization/Serializers/DecimalSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public DecimalSerializer()
5151
/// </summary>
5252
/// <param name="representation">The representation.</param>
5353
public DecimalSerializer(BsonType representation)
54-
: this(representation, new RepresentationConverter(false, false))
54+
: this(representation, new RepresentationConverter(allowOverflow: false, allowTruncation: true))
5555
{
5656
}
5757

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/BinaryExpressionToAggregationExpressionTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static AggregationExpression Translate(TranslationContext context, Binary
119119
Type t when t == typeof(ulong) => new UInt64Serializer(),
120120
Type t when t == typeof(float) => new SingleSerializer(),
121121
Type t when t == typeof(double) => new DoubleSerializer(),
122-
Type t when t == typeof(decimal) => new DecimalSerializer(),
122+
Type t when t == typeof(decimal) => DecimalSerializer.Decimal128Instance,
123123
Type { IsConstructedGenericType: true } t when t.GetGenericTypeDefinition() == typeof(Nullable<>) => (IBsonSerializer)Activator.CreateInstance(typeof(NullableSerializer<>).MakeGenericType(t.GenericTypeArguments[0])),
124124
Type { IsArray: true } t => (IBsonSerializer)Activator.CreateInstance(typeof(ArraySerializer<>).MakeGenericType(t.GetElementType())),
125125
_ => context.KnownSerializersRegistry.GetSerializer(expression) // Required for Coalesce
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Linq;
17+
using FluentAssertions;
18+
using MongoDB.Bson;
19+
using MongoDB.Bson.Serialization.Attributes;
20+
using MongoDB.Driver.Linq;
21+
using MongoDB.TestHelpers.XunitExtensions;
22+
using Xunit;
23+
24+
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira
25+
{
26+
public class CSharp4993Tests : Linq3IntegrationTest
27+
{
28+
[Theory]
29+
[ParameterAttributeData]
30+
public void Select_decimal_divide_should_work(
31+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
32+
{
33+
var collection = GetCollection(linqProvider);
34+
35+
var queryable = collection.AsQueryable()
36+
.Select(x => x.D / 1.3M);
37+
38+
var stages = Translate(collection, queryable);
39+
if (linqProvider == LinqProvider.V2)
40+
{
41+
AssertStages(stages, "{ $project : { __fld0 : { $divide : ['$D', NumberDecimal('1.3')] }, _id : 0 } }");
42+
}
43+
else
44+
{
45+
AssertStages(stages, "{ $project : { _v : { $divide : ['$D', NumberDecimal('1.3')] }, _id : 0 } }");
46+
}
47+
48+
var result = queryable.First();
49+
result.Should().Be(769.23076923076923076923076923M);
50+
}
51+
52+
private IMongoCollection<C> GetCollection(LinqProvider linqProvider)
53+
{
54+
var collection = GetCollection<C>("test", linqProvider);
55+
CreateCollection(
56+
collection,
57+
new C { Id = 1, D = 1000.0M });
58+
return collection;
59+
}
60+
61+
private class C
62+
{
63+
public int Id { get; set; }
64+
[BsonRepresentation(BsonType.Decimal128)] public decimal D { get; set; }
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)