Skip to content

Commit

Permalink
....
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 8, 2024
1 parent c67d330 commit be0358e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,8 @@ private AnyOf<Expression, Type> ParseStringLiteral(bool forceParseAsString)
_textParser.NextToken();
}

parsedStringValue = _parsingConfig.StringLiteralParsing == StringLiteralParsingType.ReplaceTwoDoubleQuotesByASingleDoubleQuote ?
StringParser.ParseStringAndReplaceTwoDoubleQuotesByASingleDoubleQuote(text, _textParser.CurrentToken.Pos) :
parsedStringValue = _parsingConfig.StringLiteralParsing == StringLiteralParsingType.EscapeDoubleQuoteByTwoDoubleQuotes ?
StringParser.ParseStringAndEscapeTwoDoubleQuotesByASingleDoubleQuote(text, _textParser.CurrentToken.Pos) :
StringParser.ParseString(text, _textParser.CurrentToken.Pos);

return _constantExpressionHelper.CreateLiteral(parsedStringValue, parsedStringValue);
Expand Down
2 changes: 1 addition & 1 deletion src/System.Linq.Dynamic.Core/Parser/StringParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal static string ParseString(string s, int pos = default)
}
}

internal static string ParseStringAndReplaceTwoDoubleQuotesByASingleDoubleQuote(string input, int position)
internal static string ParseStringAndEscapeTwoDoubleQuotesByASingleDoubleQuote(string input, int position)
{
return ReplaceTwoDoubleQuotesByASingleDoubleQuote(ParseString(input, position), position);
}
Expand Down
8 changes: 5 additions & 3 deletions src/System.Linq.Dynamic.Core/StringLiteralParsingType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
public enum StringLiteralParsingType : byte
{
/// <summary>
/// Represents the default string literal parsing type.
/// Represents the default string literal parsing type. Double quotes should be escaped using the default escaping.
/// E.G. var expression = "StaticHelper.Filter(\"UserName == \"\"x\"\"\")";
/// [Default]
/// </summary>
Default = 0,

/// <summary>
/// Represents a string literal parsing type where two consecutive double quotes are replaced by a single double quote.
/// Represents a string literal parsing type where a double quotes should be escaped by two double quotes.
/// E.G. var expression = "StaticHelper.Filter(\"UserName == \"\"x\"\"\")";
/// </summary>
ReplaceTwoDoubleQuotesByASingleDoubleQuote = 1
EscapeDoubleQuoteByTwoDoubleQuotes = 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -1483,9 +1483,9 @@ public void DynamicExpressionParser_ParseLambda_CustomType_Method_With_Expressio
resultIncome.Should().Be("Income == 5");

// Act : string
// Replace " with \"
// Replace " with \"
// Replace \" with \\\"
var _ = StaticHelper.Filter("UserName == \"x\"");
StaticHelper.Filter("UserName == \"x\"");
var expressionTextUserName = "StaticHelper.Filter(\"UserName == \\\"x\\\"\")";
var lambdaUserName = DynamicExpressionParser.ParseLambda(config, typeof(User), null, expressionTextUserName, user);
var funcUserName = (Expression<Func<User, string>>)lambdaUserName;
Expand All @@ -1497,14 +1497,15 @@ public void DynamicExpressionParser_ParseLambda_CustomType_Method_With_Expressio
resultUserName.Should().Be(@"UserName == ""x""");

// Act : string
// Replace " with ""
var configReplaceTwoDoubleQuotesByASingleDoubleQuote = new ParsingConfig
// Replace " with \"
// Replace \" with \"\"
var configNonDefault = new ParsingConfig
{
CustomTypeProvider = new TestCustomTypeProvider(),
StringLiteralParsing = StringLiteralParsingType.ReplaceTwoDoubleQuotesByASingleDoubleQuote
StringLiteralParsing = StringLiteralParsingType.EscapeDoubleQuoteByTwoDoubleQuotes
};
expressionTextUserName = "StaticHelper.Filter(\"UserName == \"\"x\"\"\")";
lambdaUserName = DynamicExpressionParser.ParseLambda(configReplaceTwoDoubleQuotesByASingleDoubleQuote, typeof(User), null, expressionTextUserName, user);
lambdaUserName = DynamicExpressionParser.ParseLambda(configNonDefault, typeof(User), null, expressionTextUserName, user);
funcUserName = (Expression<Func<User, string>>)lambdaUserName;

delegateUserName = funcUserName.Compile();
Expand Down

0 comments on commit be0358e

Please sign in to comment.