Skip to content

JavaScriptEncode for update diff #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
}