Skip to content

Commit a43d040

Browse files
committed
CSHARP-4140: Only use $getField when absolutely necessary.
1 parent 052d218 commit a43d040

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstGetFieldExpression.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,14 @@ public bool HasSafeFieldName(out string fieldName)
6767
constantFieldName.Value is BsonString stringfieldName)
6868
{
6969
fieldName = stringfieldName.Value;
70-
if (fieldName.Length > 0 && IsSafeFirstChar(fieldName[0]) && fieldName.Skip(1).All(c => IsSafeSubsequentChar(c)))
70+
if (fieldName.Length > 0 && fieldName[0] != '$' && !fieldName.Contains('.'))
7171
{
7272
return true;
7373
}
7474
}
7575

7676
fieldName = null;
7777
return false;
78-
79-
static bool IsSafeFirstChar(char c) => c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
80-
static bool IsSafeSubsequentChar(char c) => IsSafeFirstChar(c) || (c >= '0' && c <= '9');
8178
}
8279

8380
public override BsonValue Render()
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.Bson;
19+
using Xunit;
20+
21+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
22+
{
23+
public class CSharp4140Tests : Linq3IntegrationTest
24+
{
25+
[Theory]
26+
[InlineData("abc")]
27+
[InlineData("123")]
28+
[InlineData("12$")]
29+
[InlineData("!@#")]
30+
public void Should_not_use_getField(string fieldName)
31+
{
32+
var collection = GetCollection<BsonDocument>();
33+
CreateCollection(collection, new BsonDocument(fieldName, 123));
34+
var queryable = collection.AsQueryable()
35+
.Select(x => x[fieldName]);
36+
37+
var stages = Translate(collection, queryable);
38+
AssertStages(stages, $"{{ $project : {{ _v : '${fieldName}', _id : 0 }} }}");
39+
40+
var results = queryable.ToList();
41+
results.Should().Equal(123);
42+
}
43+
44+
[Theory]
45+
[InlineData("")]
46+
[InlineData(".")]
47+
[InlineData(".a")]
48+
[InlineData("a.")]
49+
public void Should_use_getField(string fieldName)
50+
{
51+
var collection = GetCollection<BsonDocument>();
52+
CreateCollection(collection, new BsonDocument(fieldName, 123));
53+
var queryable = collection.AsQueryable()
54+
.Select(x => x[fieldName]);
55+
56+
var stages = Translate(collection, queryable);
57+
AssertStages(stages, $"{{ $project : {{ _v : {{ $getField : {{ field : '{fieldName}', input : '$$ROOT' }} }}, _id : 0 }} }}");
58+
59+
var results = queryable.ToList();
60+
results.Should().Equal(123);
61+
}
62+
63+
[Theory]
64+
[InlineData("$")]
65+
[InlineData("$a")]
66+
[InlineData("$$a")]
67+
[InlineData("$a$")]
68+
public void Should_use_getField_with_literal(string fieldName)
69+
{
70+
var collection = GetCollection<BsonDocument>();
71+
CreateCollection(collection, new BsonDocument(fieldName, 123));
72+
var queryable = collection.AsQueryable()
73+
.Select(x => x[fieldName]);
74+
75+
var stages = Translate(collection, queryable);
76+
AssertStages(stages, $"{{ $project : {{ _v : {{ $getField : {{ field : {{ $literal : '{fieldName}' }}, input : '$$ROOT' }} }}, _id : 0 }} }}");
77+
78+
var results = queryable.ToList();
79+
results.Should().Equal(123);
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)