Skip to content

Commit 0b56ddd

Browse files
authored
Add config setting: NewOperatorIsNotAllowed (#732)
1 parent d5122c9 commit 0b56ddd

File tree

5 files changed

+48
-12
lines changed

5 files changed

+48
-12
lines changed

src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,10 @@ private Expression ParseIdentifier()
998998
return ParseFunctionIsNull();
999999

10001000
case KeywordsHelper.FUNCTION_NEW:
1001+
if (_parsingConfig.DisallowNewKeyword)
1002+
{
1003+
throw ParseError(Res.NewOperatorIsNotAllowed);
1004+
}
10011005
return ParseNew();
10021006

10031007
case KeywordsHelper.FUNCTION_NULLPROPAGATION:
@@ -1354,9 +1358,10 @@ private Expression ParseNew()
13541358
if (_textParser.CurrentToken.Id == TokenId.Identifier)
13551359
{
13561360
var newTypeName = _textParser.CurrentToken.Text;
1361+
13571362
_textParser.NextToken();
13581363

1359-
while (_textParser.CurrentToken.Id == TokenId.Dot || _textParser.CurrentToken.Id == TokenId.Plus)
1364+
while (_textParser.CurrentToken.Id is TokenId.Dot or TokenId.Plus)
13601365
{
13611366
var sep = _textParser.CurrentToken.Text;
13621367
_textParser.NextToken();

src/System.Linq.Dynamic.Core/ParsingConfig.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public class ParsingConfig
1414
/// <summary>
1515
/// Default ParsingConfig
1616
/// </summary>
17-
public static ParsingConfig Default { get; } = new ParsingConfig();
17+
public static ParsingConfig Default { get; } = new();
1818

1919
/// <summary>
2020
/// Default ParsingConfig for EntityFramework Core 2.1 and higher
2121
/// </summary>
22-
public static ParsingConfig DefaultEFCore21 { get; } = new ParsingConfig
22+
public static ParsingConfig DefaultEFCore21 { get; } = new()
2323
{
2424
EvaluateGroupByAtDatabase = true
2525
};
@@ -30,7 +30,7 @@ public class ParsingConfig
3030
/// <summary>
3131
/// Default ParsingConfig for CosmosDb
3232
/// </summary>
33-
public static ParsingConfig DefaultCosmosDb { get; } = new ParsingConfig
33+
public static ParsingConfig DefaultCosmosDb { get; } = new()
3434
{
3535
RenameEmptyParameterExpressionNames = true
3636
};
@@ -227,4 +227,11 @@ public IQueryableAnalyzer QueryableAnalyzer
227227
/// Default value is <c>false</c>.
228228
/// </summary>
229229
public bool SupportDotInPropertyNames { get; set; } = false;
230+
231+
/// <summary>
232+
/// Disallows the New() keyword to be used to construct a class.
233+
///
234+
/// Default value is <c>false</c>.
235+
/// </summary>
236+
public bool DisallowNewKeyword { get; set; } = false;
230237
}

src/System.Linq.Dynamic.Core/Res.cs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ internal static class Res
5454
public const string MinusCannotBeAppliedToUnsignedInteger = "'-' cannot be applied to unsigned integers.";
5555
public const string MissingAsClause = "Expression is missing an 'as' clause";
5656
public const string NeitherTypeConvertsToOther = "Neither of the types '{0}' and '{1}' converts to the other";
57+
public const string NewOperatorIsNotAllowed = "Using the new operator is not allowed via the ParsingConfig.";
5758
public const string NoApplicableAggregate = "No applicable aggregate method '{0}({1})' exists";
5859
public const string NoApplicableIndexer = "No applicable indexer exists in type '{0}'";
5960
public const string NoApplicableMethod = "No applicable method '{0}' exists in type '{1}'";

test/System.Linq.Dynamic.Core.Tests/Parser/ExpressionParserTests.MemberAccess.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Linq.Dynamic.Core.Parser;
2-
using System.Linq.Expressions;
3-
using FluentAssertions;
1+
using FluentAssertions;
42
using Xunit;
53

64
namespace System.Linq.Dynamic.Core.Tests.Parser
@@ -11,7 +9,7 @@ partial class ExpressionParserTests
119
public void ParseMemberAccess_DictionaryIndex_On_Dynamic()
1210
{
1311
// Arrange
14-
var products = (new ProductDynamic[0]).AsQueryable();
12+
var products = new ProductDynamic[0].AsQueryable();
1513

1614
// Act
1715
var expression = products.Where("Properties.Name == @0", "First Product").Expression;
@@ -31,4 +29,4 @@ public class ProductDynamic
3129

3230
public dynamic Properties { get; set; }
3331
}
34-
}
32+
}

test/System.Linq.Dynamic.Core.Tests/Parser/ExpressionParserTests.TypeAccess.cs

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq.Dynamic.Core.Exceptions;
23
using System.Linq.Dynamic.Core.Parser;
34
using System.Linq.Expressions;
45
using FluentAssertions;
@@ -216,9 +217,10 @@ public void ParseTypeAccess_Via_Constructor_DynamicType_To_String(string newExpr
216217
// Arrange
217218
var parameter = Expression.Parameter(typeof(int));
218219
var parameter2 = Expression.Parameter(typeof(int));
219-
var returnType = DynamicClassFactory.CreateType(new List<DynamicProperty> {
220-
new DynamicProperty("a", typeof(int)),
221-
new DynamicProperty("b", typeof(int))
220+
var returnType = DynamicClassFactory.CreateType(new List<DynamicProperty>
221+
{
222+
new("a", typeof(int)),
223+
new("b", typeof(int))
222224
});
223225

224226
// Act
@@ -228,4 +230,27 @@ public void ParseTypeAccess_Via_Constructor_DynamicType_To_String(string newExpr
228230
// Assert
229231
expression.ToString().Should().Match(newExpression2);
230232
}
233+
234+
[Fact]
235+
public void Parse_NewOperator_When_DisallowNewKeyword_Is_True_Should_Throw_Exception()
236+
{
237+
// Arrange
238+
var parameter = Expression.Parameter(typeof(int));
239+
var returnType = DynamicClassFactory.CreateType(new List<DynamicProperty>
240+
{
241+
new("a", typeof(int))
242+
});
243+
var config = new ParsingConfig
244+
{
245+
DisallowNewKeyword = true
246+
};
247+
248+
// Act
249+
var parser = new ExpressionParser(new[] { parameter }, "new(1 as a)", new object[] { }, config);
250+
251+
Action action = () => parser.Parse(returnType);
252+
253+
// Assert
254+
action.Should().Throw<ParseException>().Which.Message.Should().Be(Res.NewOperatorIsNotAllowed);
255+
}
231256
}

0 commit comments

Comments
 (0)