Skip to content

Commit

Permalink
normalizing json serialization settings in statemanager
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 committed Nov 21, 2024
1 parent 7783fa3 commit 8328a0c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
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

0 comments on commit 8328a0c

Please sign in to comment.