Skip to content

Commit 705e3ed

Browse files
committed
CSHARP-1430: fixed issue with OfType providing the wrong projector.
1 parent 49ecd16 commit 705e3ed

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/MongoDB.Driver.Tests/Linq/IntegrationTestBase.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ private void InsertFirst()
4545
C = new C
4646
{
4747
D = "Dexter",
48-
E = new E
48+
E = new V
4949
{
5050
F = 11,
5151
H = 22,
52-
I = new[] { "it", "icky" }
52+
I = new[] { "it", "icky" },
53+
W = 1111
5354
}
5455
},
5556
G = new[] {
@@ -220,6 +221,11 @@ public class E
220221
public IEnumerable<string> I { get; set; }
221222
}
222223

224+
public class V : E
225+
{
226+
public int W { get; set; }
227+
}
228+
223229
public interface IRoot
224230
{
225231
int Id { get; set; }

src/MongoDB.Driver.Tests/Linq/MongoQueryableTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,21 @@ public void OfType()
453453
"{ $match: { _t: 'RootDescended' } }");
454454
}
455455

456+
[Test]
457+
public void OfType_with_a_field()
458+
{
459+
var query = CreateQuery()
460+
.Select(x => x.C.E)
461+
.OfType<V>()
462+
.Where(v => v.W == 1111);
463+
464+
Assert(query,
465+
1,
466+
"{ $project: { 'C.E': 1, _id: 0 } }",
467+
"{ $match: { 'C.E._t': 'V' } }",
468+
"{ $match: { 'C.E.W': 1111 } }");
469+
}
470+
456471
[Test]
457472
public void OrderBy_method()
458473
{

src/MongoDB.Driver/Linq/Processors/Pipeline/MethodCallBinders/OfTypeBinder.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,25 @@ public Expression Bind(PipelineExpression pipeline, PipelineBindingContext bindi
3333

3434
var serializer = bindingContext.GetSerializer(newType, pipeline.Projector);
3535

36+
var projector = pipeline.Projector;
37+
var fieldProjector = projector as IFieldExpression;
38+
if (fieldProjector != null)
39+
{
40+
projector = new FieldExpression(
41+
fieldProjector.FieldName,
42+
serializer);
43+
}
44+
else
45+
{
46+
projector = new DocumentExpression(serializer);
47+
}
48+
3649
return new PipelineExpression(
3750
new WhereExpression(
3851
pipeline.Source,
3952
"__p",
4053
Expression.TypeIs(pipeline.Projector, newType)),
41-
new DocumentExpression(serializer));
54+
projector);
4255
}
4356
}
4457
}

0 commit comments

Comments
 (0)