Skip to content

Commit cb379d5

Browse files
rstamBorisDog
authored andcommitted
CSHARP-4062: Projection of constant with no known serializer is not supported.
1 parent 3eb0282 commit cb379d5

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/KnownSerializers/KnownSerializersRegistry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public IBsonSerializer GetSerializer(Expression expression, IBsonSerializer defa
4040
var possibleSerializers = _registry.TryGetValue(expression, out var knownSerializers) ? knownSerializers.GetPossibleSerializers(expressionType) : new HashSet<IBsonSerializer>();
4141
return possibleSerializers.Count switch
4242
{
43-
0 => defaultSerializer ?? throw new InvalidOperationException($"Cannot find serializer for {expression}."),
43+
0 => defaultSerializer ?? BsonSerializer.LookupSerializer(expressionType), // sometimes there is no known serializer from the context (e.g. CSHARP-4062)
4444
> 1 => throw new InvalidOperationException($"More than one possible serializer found for {expression}."),
4545
_ => possibleSerializers.First()
4646
};
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 MongoDB.Bson;
18+
using Xunit;
19+
20+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
21+
{
22+
public class CSharp4062Tests : Linq3IntegrationTest
23+
{
24+
[Fact]
25+
public void Project_anonymous_class_constant_should_work()
26+
{
27+
var collection = GetCollection<BsonDocument>();
28+
var aggregate = collection.Aggregate()
29+
.Project(x => new { R = true });
30+
31+
var stages = Translate(collection, aggregate);
32+
33+
AssertStages(stages, "{ $project : { _v : { R : true }, _id : 0 } }");
34+
}
35+
36+
[Fact]
37+
public void Project_non_anonymous_class_constant_should_work()
38+
{
39+
var collection = GetCollection<BsonDocument>();
40+
var aggregate = collection.Aggregate()
41+
.Project(x => new C { R = true });
42+
43+
var stages = Translate(collection, aggregate);
44+
45+
AssertStages(stages, "{ $project : { _v : { R : true }, _id : 0 } }");
46+
}
47+
48+
[Fact]
49+
public void Project_boolean_constant_should_work()
50+
{
51+
var collection = GetCollection<BsonDocument>();
52+
var aggregate = collection.Aggregate()
53+
.Project(x => true);
54+
55+
var stages = Translate(collection, aggregate);
56+
57+
AssertStages(stages, "{ $project : { _v : true, _id : 0 } }");
58+
}
59+
60+
[Fact]
61+
public void Project_int_constant_should_work()
62+
{
63+
var collection = GetCollection<BsonDocument>();
64+
var aggregate = collection.Aggregate()
65+
.Project(x => 1);
66+
67+
var stages = Translate(collection, aggregate);
68+
69+
AssertStages(stages, "{ $project : { _v : 1, _id : 0 } }");
70+
}
71+
72+
[Fact]
73+
public void Select_anonymous_class_constant_should_work()
74+
{
75+
var collection = GetCollection<BsonDocument>();
76+
var queryable = collection.AsQueryable()
77+
.Select(x => new { R = true });
78+
79+
var stages = Translate(collection, queryable);
80+
81+
AssertStages(stages, "{ $project : { _v : { R : true }, _id : 0 } }");
82+
}
83+
84+
[Fact]
85+
public void Select_non_anonymous_class_constant_should_work()
86+
{
87+
var collection = GetCollection<BsonDocument>();
88+
var queryable = collection.AsQueryable()
89+
.Select(x => new C { R = true });
90+
91+
var stages = Translate(collection, queryable);
92+
93+
AssertStages(stages, "{ $project : { _v : { R : true }, _id : 0 } }");
94+
}
95+
96+
[Fact]
97+
public void Select_boolean_constant_should_work()
98+
{
99+
var collection = GetCollection<BsonDocument>();
100+
var queryable = collection.AsQueryable()
101+
.Select(x => true);
102+
103+
var stages = Translate(collection, queryable);
104+
105+
AssertStages(stages, "{ $project : { _v : true, _id : 0 } }");
106+
}
107+
108+
[Fact]
109+
public void Select_int_constant_should_work()
110+
{
111+
var collection = GetCollection<BsonDocument>();
112+
var queryable = collection.AsQueryable()
113+
.Select(x => 1);
114+
115+
var stages = Translate(collection, queryable);
116+
117+
AssertStages(stages, "{ $project : { _v : 1, _id : 0 } }");
118+
}
119+
120+
private class C
121+
{
122+
public bool R { get; set; }
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)