Skip to content

Commit 0b1f14a

Browse files
rstamDmitryLukyanov
authored andcommitted
CSHARP-4521: Handle parameterName empty string the same as null.
1 parent 8bad41a commit 0b1f14a

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Misc/NameGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal class NameGenerator
2525

2626
public string GetParameterName(ParameterExpression parameter)
2727
{
28-
if (parameter.Name != null)
28+
if (!string.IsNullOrEmpty(parameter.Name))
2929
{
3030
return parameter.Name;
3131
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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;
17+
using System.Linq;
18+
using System.Linq.Expressions;
19+
using FluentAssertions;
20+
using MongoDB.Driver.Linq;
21+
using Xunit;
22+
23+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
24+
{
25+
public class CSharp4512Tests : Linq3IntegrationTest
26+
{
27+
[Theory]
28+
[InlineData("x")]
29+
[InlineData("")]
30+
[InlineData(null)]
31+
public void Where_should_work(string paramName)
32+
{
33+
var collection = CreateCollection();
34+
var param = Expression.Parameter(typeof(C), paramName);
35+
var body = Expression.MakeBinary(
36+
ExpressionType.Equal,
37+
Expression.Property(param, "Id"),
38+
Expression.Constant(1));
39+
var predicate = Expression.Lambda<Func<C, bool>>(body, param);
40+
41+
var queryable = collection.AsQueryable().Where(predicate);
42+
43+
var stages = Translate(collection, queryable);
44+
AssertStages(stages, "{ $match : { _id : 1 } }");
45+
46+
var results = queryable.ToList();
47+
results.Select(x => x.Id).Should().Equal(1);
48+
}
49+
50+
private IMongoCollection<C> CreateCollection()
51+
{
52+
var collection = GetCollection<C>("C");
53+
54+
CreateCollection(
55+
collection,
56+
new C { Id = 1 },
57+
new C { Id = 2 });
58+
59+
return collection;
60+
}
61+
62+
private class C
63+
{
64+
public int Id { get; set; }
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)