Skip to content

Commit

Permalink
JavaScriptEncode for update diff (#325)
Browse files Browse the repository at this point in the history
* JavaScriptEncode for update diff
  • Loading branch information
slorello89 authored Mar 14, 2023
1 parent bb8e138 commit 4b54057
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Redis.OM/Modeling/JsonDiff.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Web;
using Newtonsoft.Json.Linq;

namespace Redis.OM.Modeling
Expand Down Expand Up @@ -33,7 +34,7 @@ public string[] SerializeScriptArgs()
{
return _value.Type switch
{
JTokenType.String => new[] { _operation, _path, $"\"{_value}\"" },
JTokenType.String => new[] { _operation, _path, $"\"{HttpUtility.JavaScriptStringEncode(_value.ToString())}\"" },
JTokenType.Boolean => new[] { _operation, _path, _value.ToString().ToLower() },
_ => new[] { _operation, _path, _value.ToString() }
};
Expand Down
20 changes: 18 additions & 2 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ public void TestBasicQuerySpecialCharacters()
[Fact]
public void TestSave()
{
var collection = new RedisCollection<Person>(_connection, 10000);
var collection = new RedisCollection<BasicJsonObjectTestSave>(_connection, 10000);

for(var i = 0; i < 10; i++)
{
collection.Insert(new BasicJsonObjectTestSave() { Name = "TestSaveBefore" });
}
var count = 0;
foreach (var person in collection)
{
count++;
person.Name = "TestSave";
person.Mother = new Person { Name = "Diane" };
}

collection.Save();
Expand Down Expand Up @@ -1003,5 +1007,17 @@ public void TestIntSelects()
Assert.NotEmpty(res);
collection.Delete(obj);
}

[Fact]
public void TestUpdateWithQuotes()
{
var obj = new BasicJsonObject() { Name = "Bob" };
var collection = new RedisCollection<BasicJsonObject>(_connection);
collection.Insert(obj);
var reconstituted = collection.FindById(obj.Id);
reconstituted.Name = "\"Bob";
collection.Update(reconstituted);
collection.Delete(obj);
}
}
}
2 changes: 2 additions & 0 deletions test/Redis.OM.Unit.Tests/RedisSetupCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public RedisSetup()
Connection.CreateIndex(typeof(ObjectWithDateTime));
Connection.CreateIndex(typeof(ObjectWithDateTimeHash));
Connection.CreateIndex(typeof(PersonWithNestedArrayOfObject));
Connection.CreateIndex(typeof(BasicJsonObjectTestSave));
}

private IRedisConnection _connection = null;
Expand Down Expand Up @@ -60,6 +61,7 @@ public void Dispose()
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithDateTime));
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithDateTimeHash));
Connection.DropIndexAndAssociatedRecords(typeof(PersonWithNestedArrayOfObject));
Connection.DropIndexAndAssociatedRecords(typeof(BasicJsonObjectTestSave));
}
}
}
8 changes: 8 additions & 0 deletions test/Redis.OM.Unit.Tests/Serialization/BasicJsonObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ public class BasicJsonObject
[RedisIdField]
public string Id { get; set; }
public string Name { get; set; }
}

[Document(StorageType = StorageType.Json)]
public class BasicJsonObjectTestSave
{
[RedisIdField]
public string Id { get; set; }
[Indexed]public string Name { get; set; }
}

0 comments on commit 4b54057

Please sign in to comment.