Skip to content
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

normalizing json serialization settings in statemanager #505

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions src/Redis.OM/Modeling/RedisCollectionStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ namespace Redis.OM.Modeling
/// </summary>
public class RedisCollectionStateManager
{
private static JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings
private static readonly JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore, Converters = new List<JsonConverter> { new DateTimeJsonConvertNewtonsoft() },
NullValueHandling = NullValueHandling.Ignore,
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateParseHandling = DateParseHandling.DateTimeOffset,
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
Converters = new List<JsonConverter> { new DateTimeJsonConvertNewtonsoft() },
};

/// <summary>
Expand Down Expand Up @@ -82,7 +86,7 @@ internal void InsertIntoSnapshot(string key, object value)

if (DocumentAttribute.StorageType == StorageType.Json)
{
var json = JToken.FromObject(value, Newtonsoft.Json.JsonSerializer.Create(new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Converters = new List<JsonConverter> { new DateTimeJsonConvertNewtonsoft() } }));
var json = JToken.FromObject(value, Newtonsoft.Json.JsonSerializer.Create(_jsonSerializerSettings));
Snapshot.Add(key, json);
}
else
Expand Down Expand Up @@ -110,7 +114,7 @@ internal bool TryDetectDifferencesSingle(string key, object value, out IList<IOb
if (DocumentAttribute.StorageType == StorageType.Json)
{
var dataJson = JsonSerializer.Serialize(value, RedisSerializationSettings.JsonSerializerOptions);
var current = JsonConvert.DeserializeObject<JObject>(dataJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.Utc });
var current = JsonConvert.DeserializeObject<JObject>(dataJson, _jsonSerializerSettings);
var snapshot = (JToken)Snapshot[key];
var diff = FindDiff(current!, snapshot);
differences = BuildJsonDifference(diff, "$", snapshot);
Expand Down Expand Up @@ -146,7 +150,7 @@ internal IDictionary<string, IList<IObjectDiff>> DetectDifferences()
if (Data.ContainsKey(key))
{
var dataJson = JsonSerializer.Serialize(Data[key], RedisSerializationSettings.JsonSerializerOptions);
var current = JsonConvert.DeserializeObject<JObject>(dataJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
var current = JsonConvert.DeserializeObject<JObject>(dataJson, _jsonSerializerSettings);
var snapshot = (JToken)Snapshot[key];
var diff = FindDiff(current!, snapshot);
var diffArgs = BuildJsonDifference(diff, "$", snapshot);
Expand Down
44 changes: 44 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public class SearchTests
})
};

private readonly RedisReply _mockedReplyObjectWithDateTimeOffset = new[]
{
new RedisReply(1),
new RedisReply(
"Redis.OM.Unit.Tests.ObjectWithDateTimeOffsetJson:01FVN836BNQGYMT80V7RCVY73N"),
new RedisReply(new RedisReply[]
{
"$",
"{\"Id\":\"01FVN836BNQGYMT80V7RCVY73N\",\"DateTime\":1729592130000,\"Offset\":\"2024-12-25T00:00:00.000+01:00\"}"
})
};

private readonly RedisReply _mockedReplyObjectWIthMultipleByteArrays = new[]
{
new RedisReply(1),
Expand Down Expand Up @@ -1087,6 +1099,38 @@ public async Task TestUpdateJsonWithMultipleDateTimes()
Scripts.ShaCollection.Clear();
}

[Fact]
public async Task TestSaveJsonWithDateTimeOffset()
{
_substitute.ExecuteAsync("FT.SEARCH", Arg.Any<object[]>()).Returns(_mockedReplyObjectWithDateTimeOffset);

_substitute.ExecuteAsync("EVALSHA", Arg.Any<object[]>()).Returns(Task.FromResult(new RedisReply("42")));
_substitute.ExecuteAsync("SCRIPT", Arg.Any<object[]>())
.Returns(Task.FromResult(new RedisReply("cbbf1c4fab5064f419e469cc51c563f8bf51e6fb")));
var collection = new RedisCollection<ObjectWithDateTimeOffsetJson>(_substitute);
var obj = (await collection.Where(x => x.Id == "01FVN836BNQGYMT80V7RCVY73N").ToListAsync()).First();
obj.DateTime = obj.DateTime.AddMilliseconds(1);
await collection.SaveAsync();
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", new RedisKey("Redis.OM.Unit.Tests.ObjectWithDateTimeOffsetJson:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.DateTime", "1729592130001");
Scripts.ShaCollection.Clear();
}

[Fact]
public async Task TestUpdateJsonWithDateTimeOffset()
{
_substitute.ExecuteAsync("FT.SEARCH", Arg.Any<object[]>()).Returns(_mockedReplyObjectWithDateTimeOffset);

_substitute.ExecuteAsync("EVALSHA", Arg.Any<object[]>()).Returns(Task.FromResult(new RedisReply("42")));
_substitute.ExecuteAsync("SCRIPT", Arg.Any<object[]>())
.Returns(Task.FromResult(new RedisReply("cbbf1c4fab5064f419e469cc51c563f8bf51e6fb")));
var collection = new RedisCollection<ObjectWithDateTimeOffsetJson>(_substitute);
var obj = (await collection.Where(x => x.Id == "01FVN836BNQGYMT80V7RCVY73N").ToListAsync()).First();
obj.DateTime = obj.DateTime.AddMilliseconds(1);
await collection.UpdateAsync(obj);
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", new RedisKey("Redis.OM.Unit.Tests.ObjectWithDateTimeOffsetJson:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.DateTime", "1729592130001");
Scripts.ShaCollection.Clear();
}


[Fact]
public async Task TestUpdateJsonWithMultipleDateTimesHash()
Expand Down
Loading