Skip to content

Commit

Permalink
wrap all parameters in double-quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
tgmoore committed Oct 4, 2024
1 parent 7254d37 commit 8e93b30
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/Redis.OM/Aggregation/RedisAggregationSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public async ValueTask<AggregationResult<T>[]> ToArrayAsync()
=> (await ToListAsync()).ToArray();

/// <summary>
/// A string representation of the aggregation command and parameters, a serialization of the Expression.
/// A string representation of the aggregation command and parameters, a serialization of the Expression with all parameters explicitly quoted.
/// Warning: this string may not be suitable for direct execution and is intended only for use in debugging.
/// </summary>
/// <returns>A string representing the Expression serialized to an aggregation command and parameters.</returns>
Expand All @@ -132,7 +132,9 @@ public string ToQueryString()
serializedArgs.Add(_chunkSize.ToString());
}

return $"FT.AGGREGATE {string.Join(" ", serializedArgs)}";
var quotedArgs = serializedArgs.Select(arg => $"\"{arg}\"");

return $"\"FT.AGGREGATE\" {string.Join(" ", quotedArgs)}";
}

private void Initialize(RedisQueryProvider provider, Expression? expression, bool useCursor)
Expand Down
2 changes: 1 addition & 1 deletion src/Redis.OM/Searching/IRedisCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public interface IRedisCollection<T> : IOrderedQueryable<T>, IAsyncEnumerable<T>
Task<IDictionary<string, T?>> FindByIdsAsync(IEnumerable<string> ids);

/// <summary>
/// A string representation of the Redisearch command and parameters, a serialization of the Expression.
/// A string representation of the Redisearch command and parameters, a serialization of the Expression with all parameters explicitly quoted.
/// Warning: this string may not be suitable for direct execution and is intended only for use in debugging.
/// </summary>
/// <returns>A string representing the Expression serialized to a Redisearch command and parameters.</returns>
Expand Down
5 changes: 4 additions & 1 deletion src/Redis.OM/Searching/RedisCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,10 @@ public string ToQueryString()
{
var query = ExpressionTranslator.BuildQueryFromExpression(Expression, typeof(T), BooleanExpression, RootType);
query.Limit ??= new SearchLimit { Number = ChunkSize, Offset = 0 };
return $"FT.SEARCH {string.Join(" ", query.SerializeQuery())}";

var quotedArgs = query.SerializeQuery().Select(arg => $"\"{arg}\"");

return $"\"FT.SEARCH\" {string.Join(" ", quotedArgs)}";
}

private static MethodInfo GetMethodInfo<T1, T2>(Func<T1, T2> f, T1 unused)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ public async Task TestNoCursorDelete()
public void TestToQueryString()
{
var collection = new RedisAggregationSet<Person>(_substitute, true, chunkSize: 10000);
var command = "FT.AGGREGATE person-idx @Salary:[(30.55 inf] LOAD * APPLY @Address_HouseNumber + 4 AS house_num_modified SORTBY 2 @Age DESC LIMIT 0 10 WITHCURSOR COUNT 10000";
var command = "\"FT.AGGREGATE\" \"person-idx\" \"@Salary:[(30.55 inf]\" \"LOAD\" \"*\" \"APPLY\" \"@Address_HouseNumber + 4\" \"AS\" \"house_num_modified\" \"SORTBY\" \"2\" \"@Age\" \"DESC\" \"LIMIT\" \"0\" \"10\" \"WITHCURSOR\" \"COUNT\" \"10000\"";

var queryString = collection
.LoadAll()
Expand Down
2 changes: 1 addition & 1 deletion test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3894,7 +3894,7 @@ await _substitute.Received().ExecuteAsync("FT.SEARCH",
public void TestToQueryString()
{
_substitute.ClearSubstitute();
string command = "FT.SEARCH person-idx (((@Name:Ste) | (@Height:[70 inf])) (@Age:[-inf (33])) LIMIT 100 10 SORTBY Age ASC";
var command = "\"FT.SEARCH\" \"person-idx\" \"(((@Name:Ste) | (@Height:[70 inf])) (@Age:[-inf (33]))\" \"LIMIT\" \"100\" \"10\" \"SORTBY\" \"Age\" \"ASC\"";

var collection = new RedisCollection<Person>(_substitute);
var queryString = collection.Where(x => x.Name.Contains("Ste") || x.Height >= 70).Where(x => x.Age < 33).OrderBy(x => x.Age).Skip(100).Take(10).ToQueryString();
Expand Down

0 comments on commit 8e93b30

Please sign in to comment.