Skip to content

Commit

Permalink
fixing update bug with DateTimeOffsets (#373)
Browse files Browse the repository at this point in the history
* fixing update bug with DateTimeOffsets

* slightly better orginization
  • Loading branch information
slorello89 authored May 16, 2023
1 parent 3cb0aca commit 71c2173
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/Redis.OM/Modeling/JsonDiff.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Web;
using System;
using System.Linq;
using System.Text.Json;
using System.Web;
using Newtonsoft.Json.Linq;

namespace Redis.OM.Modeling
Expand Down Expand Up @@ -35,8 +38,25 @@ public string[] SerializeScriptArgs()
{
JTokenType.String => new[] { _operation, _path, $"\"{HttpUtility.JavaScriptStringEncode(_value.ToString())}\"" },
JTokenType.Boolean => new[] { _operation, _path, _value.ToString().ToLower() },
JTokenType.Date => SerializeAsDateTime(),
_ => new[] { _operation, _path, _value.ToString() }
};
}

private string[] SerializeAsDateTime()
{
var jValue = (JValue)_value;
if (jValue.Value is DateTimeOffset)
{
return new[]
{
_operation,
_path,
$"{JsonSerializer.Serialize(_value.Value<DateTimeOffset>())}",
};
}

return new[] { _operation, _path, $"{JsonSerializer.Serialize(_value.Value<DateTime>())}" };
}
}
}
2 changes: 1 addition & 1 deletion src/Redis.OM/Modeling/RedisCollectionStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,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 });
var current = JsonConvert.DeserializeObject<JObject>(dataJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.Utc });
var snapshot = (JToken)Snapshot[key];
var diff = FindDiff(current!, snapshot);
differences = BuildJsonDifference(diff, "$", snapshot);
Expand Down
20 changes: 20 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,5 +1113,25 @@ public void TestSelectOnEmbeddedDocuments()
Assert.Equal("World",resNoNew.InnerInnerCascade.Tag);
Assert.Equal(42,resNoNew.InnerInnerCascade.Num);
}

[Fact]
public void SaveDateTimeOffset()
{
var obj = new ObjectWithDateTimeOffsetJson
{
Offset = DateTimeOffset.Now,
DateTime = DateTime.Now
};
var collection = new RedisCollection<ObjectWithDateTimeOffsetJson>(_connection);
collection.Insert(obj);

var intermediate = collection.First(x => x.Id == obj.Id);
intermediate.Offset = intermediate.Offset.AddMinutes(10);
intermediate.DateTime = intermediate.DateTime.AddHours(1);
collection.Update(intermediate);
var final = collection.First(x => x.Id == obj.Id);
Assert.Equal(intermediate.Offset,final.Offset);
Assert.Equal(intermediate.DateTime,final.DateTime);
}
}
}
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 @@ -29,6 +29,7 @@ public RedisSetup()
Connection.CreateIndex(typeof(ComplexObjectWithCascadeAndJsonPath));
Connection.CreateIndex(typeof(BasicJsonObjectTestSave));
Connection.CreateIndex(typeof(SelectTestObject));
Connection.CreateIndex(typeof(ObjectWithDateTimeOffsetJson));
}

private IRedisConnection _connection = null;
Expand Down Expand Up @@ -66,6 +67,7 @@ public void Dispose()
Connection.DropIndexAndAssociatedRecords(typeof(ComplexObjectWithCascadeAndJsonPath));
Connection.DropIndexAndAssociatedRecords(typeof(BasicJsonObjectTestSave));
Connection.DropIndexAndAssociatedRecords(typeof(SelectTestObject));
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithDateTimeOffsetJson));
}
}
}
11 changes: 11 additions & 0 deletions test/Redis.OM.Unit.Tests/Serialization/ObjectWithDateTimeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@ public class ObjectWithDateTimeOffset
{
public DateTimeOffset Offset { get; set; }
}

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

public DateTimeOffset Offset { get; set; }
public DateTime DateTime { get; set; }
}
}

0 comments on commit 71c2173

Please sign in to comment.