Skip to content

Commit

Permalink
Add configuration settting AreContextSymbolsEnabled
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Aug 13, 2023
1 parent 06d4110 commit 118ffc3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ public KeywordsHelper(ParsingConfig config)
_keywords.Add(KEYWORD_ROOT, KEYWORD_ROOT);
}

_keywords.Add(SYMBOL_IT, SYMBOL_IT);
_keywords.Add(SYMBOL_PARENT, SYMBOL_PARENT);
_keywords.Add(SYMBOL_ROOT, SYMBOL_ROOT);
if (config.AreContextSymbolsEnabled)
{
_keywords.Add(SYMBOL_IT, SYMBOL_IT);
_keywords.Add(SYMBOL_PARENT, SYMBOL_PARENT);
_keywords.Add(SYMBOL_ROOT, SYMBOL_ROOT);
}

_keywords.Add(FUNCTION_IIF, FUNCTION_IIF);
_keywords.Add(FUNCTION_ISNULL, FUNCTION_ISNULL);
Expand Down
10 changes: 9 additions & 1 deletion src/System.Linq.Dynamic.Core/ParsingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,18 @@ public IQueryableAnalyzer QueryableAnalyzer
/// Determines if the context keywords (it, parent, and root) are valid and usable inside a Dynamic Linq string expression.
/// Does not affect the usability of the equivalent context symbols ($, ^ and ~).
///
/// Default value is <c>false</c>.
/// Default value is <c>true</c>.
/// </summary>
public bool AreContextKeywordsEnabled { get; set; } = true;

/// <summary>
/// Determines if the context symbols ($, ^ and ~) are valid and usable inside a Dynamic Linq string expression.
/// Does not affect the usability of the equivalent context keywords (it, parent, and root).
///
/// Default value is <c>true</c>.
/// </summary>
public bool AreContextSymbolsEnabled { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether the EntityFramework version supports evaluating GroupBy at database level.
/// See https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.1#linq-groupby-translation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,44 @@ public void Parse_When_PrioritizePropertyOrFieldOverTheType_IsFalse(string expre
// Assert
parsedExpression.Should().StartWith(result);
}

[Theory]
[InlineData("it != null")]
[InlineData("parent != null")]
[InlineData("root != null")]
public void Parse_When_AreContextKeywordsEnabled_IsFalse_Should_Not_SupportKeywords(string expression)
{
// Arrange
var config = new ParsingConfig
{
AreContextKeywordsEnabled = false
};
ParameterExpression[] parameters = { ParameterExpressionHelper.CreateParameterExpression(typeof(Company), "company") };

// Act
Action a = () => new ExpressionParser(parameters, expression, null, config).Parse(null);

// Assert
a.Should().Throw<ParseException>().And.Message.Should().NotBeEmpty();
}

[Theory]
[InlineData("$ != null")]
[InlineData("^ != null")]
[InlineData("~ != null")]
public void Parse_When_AreContextSymbolsEnabled_IsFalse_Should_Not_SupportKeywords(string expression)
{
// Arrange
var config = new ParsingConfig
{
AreContextSymbolsEnabled = false
};
ParameterExpression[] parameters = { ParameterExpressionHelper.CreateParameterExpression(typeof(Company), "company") };

// Act
Action a = () => new ExpressionParser(parameters, expression, null, config).Parse(null);

// Assert
a.Should().Throw<ParseException>().And.Message.Should().NotBeEmpty();
}
}

0 comments on commit 118ffc3

Please sign in to comment.