Skip to content

Commit 389ae9a

Browse files
rstamBorisDog
authored andcommitted
CSHARP-4126: Replace parameter names that aren't valid server variable names with generated variable names.
1 parent ae161bc commit 389ae9a

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public string GetVarName(string symbolName)
4040
return symbolName;
4141
}
4242

43-
return $"_v{_varCounter++}";
43+
return $"v__{_varCounter++}";
4444

4545
static bool IsValidVarName(string name)
4646
{
@@ -52,7 +52,7 @@ static bool IsValidVarName(string name)
5252

5353
static bool IsValidFirstChar(char c)
5454
{
55-
return c == '_' || IsBetween(c, 'a', 'z') || IsBetween(c, 'A', 'Z');
55+
return IsBetween(c, 'a', 'z');
5656
}
5757

5858
static bool IsValidSubsequentChar(char c)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 FluentAssertions;
19+
using Xunit;
20+
21+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
22+
{
23+
public class CSharp4126Tests : Linq3IntegrationTest
24+
{
25+
[Fact]
26+
public void Test()
27+
{
28+
var collection = GetCollection<C>();
29+
CreateCollection(
30+
collection,
31+
new C { Id = 1, A = new[] { 1, -2, -3 } });
32+
33+
var queryable = collection.AsQueryable()
34+
.Select(_p0 => new { Id = _p0.Id, A = _p0.A.Select(_p1 => Math.Abs(_p1)) });
35+
36+
var stages = Translate(collection, queryable);
37+
AssertStages(stages, "{ $project : { Id : '$_id', A : { $map : { input : '$A', as : 'v__0', in : { $abs : '$$v__0' } } }, _id : 0 } }");
38+
39+
var results = queryable.ToList();
40+
results.Should().HaveCount(1);
41+
results[0].Id.Should().Be(1);
42+
results[0].A.Should().Equal(1, 2, 3);
43+
}
44+
45+
public class C
46+
{
47+
public int Id { get; set; }
48+
public int[] A { get; set; }
49+
}
50+
}
51+
}

tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationTests/Linq3IntegrationTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ protected void CreateCollection<TDocument>(IMongoCollection<TDocument> collectio
5151
}
5252
}
5353

54+
protected void CreateCollection<TDocument>(IMongoCollection<TDocument> collection, params TDocument[] documents)
55+
{
56+
CreateCollection(collection, (IEnumerable<TDocument>)documents); ;
57+
}
58+
5459
protected IMongoCollection<TDocument> GetCollection<TDocument>(string collectionName = null)
5560
{
5661
var databaseName = DriverTestConfiguration.DatabaseNamespace.DatabaseName;

0 commit comments

Comments
 (0)