Skip to content

Commit 11c8da8

Browse files
VladimirJamesKovacs
authored andcommitted
CSHARP-4316: Fixed translation of types similar to Nullable<T>
1 parent 62829f7 commit 11c8da8

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
2727
using MongoDB.Driver.Linq.Linq3Implementation.Serializers;
2828
using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.PropertyTranslators;
29+
using MongoDB.Driver.Support;
2930

3031
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators
3132
{
@@ -36,7 +37,7 @@ public static AggregationExpression Translate(TranslationContext context, Member
3637
var containerExpression = expression.Expression;
3738
var member = expression.Member;
3839

39-
if (member is PropertyInfo property)
40+
if (member is PropertyInfo property && property.DeclaringType.IsNullable())
4041
{
4142
switch (property.Name)
4243
{
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Linq;
2+
using Xunit;
3+
4+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
5+
{
6+
public class CSharp4316Tests : Linq3IntegrationTest
7+
{
8+
[Fact]
9+
public void Nullable_type_translate_should_work()
10+
{
11+
var collection = GetCollection<ModelContainNullableType>();
12+
var fluent = collection.Aggregate()
13+
.Group(x => new { Some = x.Some.Value, x.AnotherKeyGroup, x.Some.HasValue }, x => x.Select(t => t));
14+
15+
var stages = Translate(collection, fluent);
16+
var expected = new []
17+
{
18+
"{ $group : {'_id' : { Some : '$Some', AnotherKeyGroup : '$AnotherKeyGroup', HasValue : { $ne : ['$Some', null] }}, __agg0 : { '$push' : '$$ROOT'}}}",
19+
"{ $project : { '_v' : '$__agg0', _id : 0 } }"
20+
};
21+
22+
AssertStages(stages, expected);
23+
}
24+
25+
[Fact]
26+
public void Type_contains_property_value_translate_should_work()
27+
{
28+
var collection = GetCollection<ModelWithoutNullable>();
29+
var fluent = collection.Aggregate()
30+
.Group(x => new { Key1 = x.Property.Value, Key2 = x.SomeData, Key3 = x.Property.HasValue}, x => x.Select(t => t));
31+
32+
var stages = Translate(collection, fluent);
33+
var expected = new []
34+
{
35+
"{ $group : {'_id' : { Key1 : '$Property.Value', Key2 : '$SomeData', Key3 : '$Property.HasValue' }, __agg0 : { '$push' : '$$ROOT'}}}",
36+
"{ $project : { '_v' : '$__agg0', _id : 0 } }"
37+
};
38+
AssertStages(stages, expected);
39+
}
40+
41+
public class ModelContainNullableType
42+
{
43+
public int? Some { get; set; }
44+
public float AnotherKeyGroup { get; set; }
45+
46+
}
47+
48+
public class ModelWithoutNullable
49+
{
50+
public CustomTypeLikeNullable Property { get; set; }
51+
public int SomeData { get; set; }
52+
}
53+
54+
public class CustomTypeLikeNullable
55+
{
56+
public string Value { get; set; }
57+
public bool HasValue { get; set; }
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)