Skip to content

Commit 4b54057

Browse files
authored
JavaScriptEncode for update diff (#325)
* JavaScriptEncode for update diff
1 parent bb8e138 commit 4b54057

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

src/Redis.OM/Modeling/JsonDiff.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Newtonsoft.Json;
2+
using System.Web;
23
using Newtonsoft.Json.Linq;
34

45
namespace Redis.OM.Modeling
@@ -33,7 +34,7 @@ public string[] SerializeScriptArgs()
3334
{
3435
return _value.Type switch
3536
{
36-
JTokenType.String => new[] { _operation, _path, $"\"{_value}\"" },
37+
JTokenType.String => new[] { _operation, _path, $"\"{HttpUtility.JavaScriptStringEncode(_value.ToString())}\"" },
3738
JTokenType.Boolean => new[] { _operation, _path, _value.ToString().ToLower() },
3839
_ => new[] { _operation, _path, _value.ToString() }
3940
};

test/Redis.OM.Unit.Tests/RediSearchTests/SearchFunctionalTests.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,17 @@ public void TestBasicQuerySpecialCharacters()
148148
[Fact]
149149
public void TestSave()
150150
{
151-
var collection = new RedisCollection<Person>(_connection, 10000);
151+
var collection = new RedisCollection<BasicJsonObjectTestSave>(_connection, 10000);
152+
153+
for(var i = 0; i < 10; i++)
154+
{
155+
collection.Insert(new BasicJsonObjectTestSave() { Name = "TestSaveBefore" });
156+
}
152157
var count = 0;
153158
foreach (var person in collection)
154159
{
155160
count++;
156161
person.Name = "TestSave";
157-
person.Mother = new Person { Name = "Diane" };
158162
}
159163

160164
collection.Save();
@@ -1003,5 +1007,17 @@ public void TestIntSelects()
10031007
Assert.NotEmpty(res);
10041008
collection.Delete(obj);
10051009
}
1010+
1011+
[Fact]
1012+
public void TestUpdateWithQuotes()
1013+
{
1014+
var obj = new BasicJsonObject() { Name = "Bob" };
1015+
var collection = new RedisCollection<BasicJsonObject>(_connection);
1016+
collection.Insert(obj);
1017+
var reconstituted = collection.FindById(obj.Id);
1018+
reconstituted.Name = "\"Bob";
1019+
collection.Update(reconstituted);
1020+
collection.Delete(obj);
1021+
}
10061022
}
10071023
}

test/Redis.OM.Unit.Tests/RedisSetupCollection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public RedisSetup()
2626
Connection.CreateIndex(typeof(ObjectWithDateTime));
2727
Connection.CreateIndex(typeof(ObjectWithDateTimeHash));
2828
Connection.CreateIndex(typeof(PersonWithNestedArrayOfObject));
29+
Connection.CreateIndex(typeof(BasicJsonObjectTestSave));
2930
}
3031

3132
private IRedisConnection _connection = null;
@@ -60,6 +61,7 @@ public void Dispose()
6061
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithDateTime));
6162
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithDateTimeHash));
6263
Connection.DropIndexAndAssociatedRecords(typeof(PersonWithNestedArrayOfObject));
64+
Connection.DropIndexAndAssociatedRecords(typeof(BasicJsonObjectTestSave));
6365
}
6466
}
6567
}

test/Redis.OM.Unit.Tests/Serialization/BasicJsonObject.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ public class BasicJsonObject
88
[RedisIdField]
99
public string Id { get; set; }
1010
public string Name { get; set; }
11+
}
12+
13+
[Document(StorageType = StorageType.Json)]
14+
public class BasicJsonObjectTestSave
15+
{
16+
[RedisIdField]
17+
public string Id { get; set; }
18+
[Indexed]public string Name { get; set; }
1119
}

0 commit comments

Comments
 (0)