Skip to content

Commit 1ac46bd

Browse files
committed
CSHARP-1777: Fix regression in filter builder with Contains in an expression.
1 parent 1a64362 commit 1ac46bd

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

src/MongoDB.Driver/Linq/Processors/Pipeline/PipelineBindingContext.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2015 MongoDB Inc.
1+
/* Copyright 2015-2017 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -92,11 +92,16 @@ public IBsonSerializer GetSerializer(Type type, Expression node)
9292
serializer = _serializerRegistry.GetSerializer(type);
9393
if (node != null)
9494
{
95-
var childConfigurable = serializer as IChildSerializerConfigurable;
96-
if (childConfigurable != null)
95+
var childConfigurableSerializer = serializer as IChildSerializerConfigurable;
96+
if (childConfigurableSerializer != null)
9797
{
98-
var childSerializer = GetSerializer(node.Type, node);
99-
serializer = SerializerHelper.RecursiveConfigureChildSerializer(childConfigurable, childSerializer);
98+
var nodeSerializer = GetSerializer(node.Type, node);
99+
var deepestChildSerializer = SerializerHelper.GetDeepestChildSerializer(childConfigurableSerializer);
100+
101+
if (nodeSerializer.ValueType == deepestChildSerializer.ValueType)
102+
{
103+
serializer = SerializerHelper.RecursiveConfigureChildSerializer(childConfigurableSerializer, nodeSerializer);
104+
}
100105
}
101106
}
102107
}

src/MongoDB.Driver/Linq/SerializerHelper.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2015-2016 MongoDB Inc.
1+
/* Copyright 2015-2017 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -72,5 +72,18 @@ public static IBsonSerializer RecursiveConfigureChildSerializer(IChildSerializer
7272

7373
return configurable.WithChildSerializer(childSerializer);
7474
}
75+
76+
public static IBsonSerializer GetDeepestChildSerializer(IChildSerializerConfigurable childConfigurableSerializer)
77+
{
78+
IBsonSerializer deepestChildSerializer = null;
79+
80+
while (childConfigurableSerializer != null)
81+
{
82+
deepestChildSerializer = childConfigurableSerializer.ChildSerializer;
83+
childConfigurableSerializer = deepestChildSerializer as IChildSerializerConfigurable;
84+
}
85+
86+
return deepestChildSerializer;
87+
}
7588
}
7689
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright 2017 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.Collections.Generic;
17+
using System.Linq;
18+
using FluentAssertions;
19+
using MongoDB.Bson;
20+
using MongoDB.Bson.Serialization;
21+
using MongoDB.Bson.Serialization.Attributes;
22+
using Xunit;
23+
24+
namespace MongoDB.Driver.Tests
25+
{
26+
public class FilterDefinitionBuilderEnumerableNullableEnumContainsNullableEnumTests
27+
{
28+
private static IBsonSerializerRegistry __registry = BsonSerializer.SerializerRegistry;
29+
private static IBsonSerializer<C> __serializer = BsonSerializer.SerializerRegistry.GetSerializer<C>();
30+
private static FilterDefinitionBuilder<C> __subject = Builders<C>.Filter;
31+
32+
public enum E { A, B };
33+
34+
public class C
35+
{
36+
public int Id;
37+
[BsonRepresentation(BsonType.String)]
38+
public E? P;
39+
}
40+
41+
[Fact]
42+
public void Contains_should_render_correctly()
43+
{
44+
var values = (IEnumerable<E?>)new E?[] { null, E.A, E.B };
45+
var filter = __subject.Where(x => values.Contains(x.P));
46+
47+
var result = filter.Render(__serializer, __registry);
48+
49+
result.Should().Be("{ P : { $in : [ null, 'A', 'B' ] } }");
50+
}
51+
}
52+
}

tests/MongoDB.Driver.Tests/MongoDB.Driver.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
<Compile Include="FilterDefinitionBuilderEnumComparedToEnumWithStringRepresentationTests.cs" />
113113
<Compile Include="FilterDefinitionBuilderEnumComparedToNullableEnumTests.cs" />
114114
<Compile Include="FilterDefinitionBuilderEnumComparedToNullableEnumWithStringRepresentationTests.cs" />
115+
<Compile Include="FilterDefinitionBuilderEnumerableNullableEnumContainsNullableEnumTests.cs" />
115116
<Compile Include="FilterDefinitionBuilderIntArrayComparedToEnumerableIntTests.cs" />
116117
<Compile Include="FilterDefinitionBuilderEnumComparedToEnumTests.cs" />
117118
<Compile Include="FilterDefinitionBuilderIntComparedToIntTests.cs" />

0 commit comments

Comments
 (0)