Skip to content

Commit e154014

Browse files
rstamdnickless
authored andcommitted
CSHARP-4304: Handle conversions to an interface.
1 parent 494c048 commit e154014

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,20 @@ public static AggregationExpression Translate(TranslationContext context, UnaryE
4646
}
4747
}
4848

49-
var ast = AstExpression.Convert(operandTranslation.Ast, expressionType);
50-
var serializer = context.KnownSerializersRegistry.GetSerializer(expression);
49+
var ast = operandTranslation.Ast;
50+
IBsonSerializer serializer;
51+
if (expressionType.IsInterface)
52+
{
53+
// when an expression is cast to an interface it's a no-op as far as we're concerned
54+
// and we can just use the serializer for the concrete type and members not defined in the interface will just be ignored
55+
serializer = operandTranslation.Serializer;
56+
}
57+
else
58+
{
59+
ast = AstExpression.Convert(ast, expressionType);
60+
serializer = context.KnownSerializersRegistry.GetSerializer(expression);
61+
}
62+
5163
return new AggregationExpression(expression, ast, serializer);
5264
}
5365

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.Driver.Linq;
19+
using Xunit;
20+
21+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
22+
{
23+
public class CSharp4304Tests : Linq3IntegrationTest
24+
{
25+
[Fact]
26+
public void Example_should_work()
27+
{
28+
var collection = GetCollection<Parent>();
29+
var database = collection.Database;
30+
31+
var queryable = GetChildrenQueryable<Child>(database, "Child");
32+
33+
var stages = Translate(collection, queryable);
34+
AssertStages(
35+
stages,
36+
"{ $project : { _outer : '$$ROOT', _id : 0 } }",
37+
"{ $lookup : { from : 'Child', localField : '_outer.ChildId', foreignField : '_id', as : '_inner' } }",
38+
"{ $unwind : '$_inner' }",
39+
"{ $project : { _v : '$_inner', _id : 0 } }");
40+
41+
CreateParentCollection();
42+
CreateChildCollection();
43+
var results = queryable.ToList();
44+
results.OrderBy(r => r.Id).Select(r => r.Id).Should().Equal("11", "22");
45+
}
46+
47+
private IMongoQueryable<TChild> GetChildrenQueryable<TChild>(IMongoDatabase db, string childCollectionName) where TChild : IEntity
48+
{
49+
var parentCollection = db.GetCollection<Parent>("Parent");
50+
var childCollection = db.GetCollection<TChild>(childCollectionName).AsQueryable();
51+
52+
return parentCollection
53+
.AsQueryable()
54+
.Join(
55+
childCollection,
56+
p => p.ChildId,
57+
c => c.Id,
58+
(_, c) => c);
59+
}
60+
61+
private void CreateParentCollection()
62+
{
63+
var parentCollection = GetCollection<Parent>("Parent");
64+
65+
CreateCollection(
66+
parentCollection,
67+
new Parent { Id = "1", ChildId = "11" },
68+
new Parent { Id = "2", ChildId = "22" });
69+
}
70+
71+
private void CreateChildCollection()
72+
{
73+
var childCollection = GetCollection<Child>("Child");
74+
75+
CreateCollection(
76+
childCollection,
77+
new Child { Id = "11" },
78+
new Child { Id = "22" });
79+
}
80+
81+
public interface IEntity
82+
{
83+
string Id { get; set; }
84+
}
85+
86+
public class Parent : IEntity
87+
{
88+
public string Id { get; set; }
89+
public string ChildId { get; set; }
90+
}
91+
92+
public class Child : IEntity
93+
{
94+
public string Id { get; set; }
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)