Skip to content
Browse files

Escape special characters in regular expressions generated for LINQ q…

…ueries involving strings.
  • Loading branch information...
1 parent 2b9e193 commit 2caf8881d873101f15a72e987d4ac51e7061c1ee rstam committed Apr 9, 2012
Showing with 28 additions and 6 deletions.
  1. +2 −3 Driver/Linq/Translators/SelectQuery.cs
  2. +26 −3 DriverUnitTests/Linq/SelectQueryTests.cs
View
5 Driver/Linq/Translators/SelectQuery.cs
@@ -824,7 +824,7 @@ private IMongoQuery BuildStringQuery(MethodCallExpression methodCallExpression)
return null;
}
- var pattern = (string)constantExpression.Value; // TODO: escape value
+ var pattern = Regex.Escape((string)constantExpression.Value);
switch (methodCallExpression.Method.Name)
{
case "Contains": pattern = ".*" + pattern + ".*"; break;
@@ -1103,8 +1103,7 @@ private string GetTrimCharsPattern(Expression trimCharsExpression)
}
else
{
- // TODO: handle special characters better
- sb.Append(c.ToString());
+ sb.Append(Regex.Escape(c.ToString()));
}
}
if (sawDash)
View
29 DriverUnitTests/Linq/SelectQueryTests.cs
@@ -4260,6 +4260,29 @@ public void TestWhereSContainsAbcNot()
}
[Test]
+ public void TestWhereSContainsDot()
+ {
+ var query = from c in _collection.AsQueryable<C>()
+ where c.S.Contains(".")
+ select c;
+
+ var translatedQuery = MongoQueryTranslator.Translate(query);
+ Assert.IsInstanceOf<SelectQuery>(translatedQuery);
+ Assert.AreSame(_collection, translatedQuery.Collection);
+ Assert.AreSame(typeof(C), translatedQuery.DocumentType);
+
+ var selectQuery = (SelectQuery)translatedQuery;
+ Assert.AreEqual("(C c) => c.S.Contains(\".\")", ExpressionFormatter.ToString(selectQuery.Where));
+ Assert.IsNull(selectQuery.OrderBy);
+ Assert.IsNull(selectQuery.Projection);
+ Assert.IsNull(selectQuery.Skip);
+ Assert.IsNull(selectQuery.Take);
+
+ Assert.AreEqual("{ \"s\" : /\\./s }", selectQuery.BuildQuery().ToJson());
+ Assert.AreEqual(0, Consume(query));
+ }
+
+ [Test]
public void TestWhereSEndsWithAbc()
{
var query = from c in _collection.AsQueryable<C>()
@@ -4794,7 +4817,7 @@ public void TestWhereSTrimStartsWithXyzNot()
public void TestWhereSTrimStartTrimEndToLowerContainsXyz()
{
var query = from c in _collection.AsQueryable<C>()
- where c.S.TrimStart(' ', '-', '\t').TrimEnd().ToLower().Contains("xyz")
+ where c.S.TrimStart(' ', '.', '-', '\t').TrimEnd().ToLower().Contains("xyz")
select c;
var translatedQuery = MongoQueryTranslator.Translate(query);
@@ -4803,13 +4826,13 @@ where c.S.TrimStart(' ', '-', '\t').TrimEnd().ToLower().Contains("xyz")
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
var selectQuery = (SelectQuery)translatedQuery;
- Assert.AreEqual("(C c) => c.S.TrimStart(Char[]:{ ' ', '-', '\t' }).TrimEnd(Char[]:{ }).ToLower().Contains(\"xyz\")", ExpressionFormatter.ToString(selectQuery.Where));
+ Assert.AreEqual("(C c) => c.S.TrimStart(Char[]:{ ' ', '.', '-', '\t' }).TrimEnd(Char[]:{ }).ToLower().Contains(\"xyz\")", ExpressionFormatter.ToString(selectQuery.Where));
Assert.IsNull(selectQuery.OrderBy);
Assert.IsNull(selectQuery.Projection);
Assert.IsNull(selectQuery.Skip);
Assert.IsNull(selectQuery.Take);
- Assert.AreEqual("{ \"s\" : /^[ \t-]*.*xyz.*\\s*$/is }", selectQuery.BuildQuery().ToJson());
+ Assert.AreEqual("{ \"s\" : /^[\\ \\.\\t-]*.*xyz.*\\s*$/is }", selectQuery.BuildQuery().ToJson());
Assert.AreEqual(1, Consume(query));
}

0 comments on commit 2caf888

Please sign in to comment.
Something went wrong with that request. Please try again.