Skip to content

Commit 14e7b87

Browse files
authored
CSHARP-4423: LinqProvider.V3 Odata filter problem with id filter (#1130)
1 parent 0dd40f6 commit 14e7b87

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public static AggregationExpression Translate(TranslationContext context, UnaryE
3939
var operandExpression = expression.Operand;
4040
var operandTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, operandExpression);
4141

42+
if (expressionType == operandExpression.Type)
43+
{
44+
return operandTranslation;
45+
}
46+
4247
if (IsConvertEnumToUnderlyingType(expression))
4348
{
4449
return TranslateConvertEnumToUnderlyingType(expression, operandTranslation);

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ToFilterFieldTranslators/ConvertExpressionToFilterFieldTranslator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public static AstFilterField Translate(TranslationContext context, UnaryExpressi
3434
var fieldType = field.Serializer.ValueType;
3535
var targetType = expression.Type;
3636

37+
if (targetType == fieldType)
38+
{
39+
return field;
40+
}
41+
3742
if (IsConvertEnumToUnderlyingType(fieldType, targetType))
3843
{
3944
return TranslateConvertEnumToUnderlyingType(field, targetType);

tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ConvertExpressionToFilterFieldTranslatorTests.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515

1616
using FluentAssertions;
17+
using MongoDB.Bson;
18+
using MongoDB.Bson.Serialization.Attributes;
1719
using MongoDB.Driver.Linq;
1820
using Xunit;
1921

@@ -22,7 +24,7 @@ namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Translators.ExpressionTo
2224
public class ConvertExpressionToFilterFieldTranslatorTests : Linq3IntegrationTest
2325
{
2426
[Fact]
25-
public void Should_translate_to_derived_class_on_method_call()
27+
public void Projection_using_convert_to_derived_class_with_field_expression_should_work()
2628
{
2729
var collection = GetCollection();
2830

@@ -39,12 +41,11 @@ public void Should_translate_to_derived_class_on_method_call()
3941
"{ '$project' : { _id : '$_id', A : { '$toUpper' : '$A' } } }");
4042

4143
var result = queryable.Single();
42-
result.Id.Should().Be(1);
4344
result.A.Should().Be("TEST VALUE");
4445
}
4546

4647
[Fact]
47-
public void Should_translate_to_derived_class_on_projection()
48+
public void Projection_using_convert_to_derived_class_should_work()
4849
{
4950
var collection = GetCollection();
5051

@@ -61,24 +62,41 @@ public void Should_translate_to_derived_class_on_projection()
6162
"{ '$project' : { _id : '$_id', A : '$A' } }");
6263

6364
var result = queryable.Single();
64-
result.Id.Should().Be(1);
6565
result.A.Should().Be("test value");
6666
}
6767

68+
[Fact]
69+
public void Filter_using_string_as_objectid_should_work()
70+
{
71+
var collection = GetCollection();
72+
var queryable = collection.AsQueryable()
73+
// do not remove this extra unnecessary convert to string, it's important to reproduce the issue
74+
.Where(i => ((string)i.Id) == "000000000000000000000001");
75+
76+
var stages = Translate(collection, queryable);
77+
AssertStages(
78+
stages,
79+
"{ $match : { _id : ObjectId('000000000000000000000001') } }");
80+
81+
var result = queryable.Single();
82+
result.Id.Should().Be("000000000000000000000001");
83+
}
84+
6885
private IMongoCollection<BaseClass> GetCollection()
6986
{
7087
var collection = GetCollection<BaseClass>("test");
7188
CreateCollection(collection, new DerivedClass()
7289
{
73-
Id = 1,
90+
Id = "000000000000000000000001",
7491
A = "test value"
7592
});
7693
return collection;
7794
}
7895

7996
private class BaseClass
8097
{
81-
public int Id { get; set; }
98+
[BsonRepresentation(BsonType.ObjectId)]
99+
public string Id { get; set; }
82100
}
83101

84102
private class DerivedClass : BaseClass

0 commit comments

Comments
 (0)