Skip to content

Commit

Permalink
378 Fix problem with IDateTimeContent BSON serialization/deserializat…
Browse files Browse the repository at this point in the history
…ion (#379)
  • Loading branch information
dzmitryk-kontent-ai authored Aug 4, 2023
1 parent 4f00f7e commit c712ba7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Kontent.Ai.Delivery.Caching.Tests/BinarySerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public async void GetItemsAsync_SerializeAndDeserialize()
Assert.NotEmpty(firstDeserializedItem.TeaserImage);
Assert.NotEmpty(firstDeserializedItem.Personas);
Assert.Equal(firstItem.PostDate, firstDeserializedItem.PostDate);
Assert.Equal(firstItem.PostDate.Value.Kind, firstDeserializedItem.PostDate.Value.Kind);
Assert.Equal(firstItem.PostDateContent.DisplayTimezone, firstDeserializedItem.PostDateContent.DisplayTimezone);
Assert.Equal(firstItem.PostDateContent.Value, firstDeserializedItem.PostDateContent.Value);
Assert.Equal(firstItem.PostDateContent.Value.Value.Kind, firstDeserializedItem.PostDateContent.Value.Value.Kind);
}
}
}
5 changes: 5 additions & 0 deletions Kontent.Ai.Delivery.Caching.Tests/ContentTypes/Article.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Kontent.Ai.Delivery.Abstractions;
using Kontent.Ai.Delivery.ContentItems.Attributes;

namespace Kontent.Ai.Delivery.Caching.Tests.ContentTypes
{
public partial class Article
{
[PropertyName("post_date")]
public IDateTimeContent PostDateContent { get; set; }
}
}
30 changes: 20 additions & 10 deletions Kontent.Ai.Delivery.Caching.Tests/Fixtures/full_articles.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"post_date": {
"type": "date_time",
"name": "Post date",
"value": "2014-11-18T00:00:00Z"
"value": "2014-11-18T00:00:00Z",
"display_timezone": "Europe/Prague"
},
"summary": {
"type": "text",
Expand Down Expand Up @@ -208,7 +209,8 @@
"post_date": {
"type": "date_time",
"name": "Post date",
"value": "2014-11-02T00:00:00Z"
"value": "2014-11-02T00:00:00Z",
"display_timezone": "Europe/Prague"
},
"summary": {
"type": "text",
Expand Down Expand Up @@ -390,7 +392,8 @@
"post_date": {
"type": "date_time",
"name": "Post date",
"value": "2014-11-12T00:00:00Z"
"value": "2014-11-12T00:00:00Z",
"display_timezone": "Europe/Prague"
},
"summary": {
"type": "text",
Expand Down Expand Up @@ -554,7 +557,8 @@
"post_date": {
"type": "date_time",
"name": "Post date",
"value": "2014-11-07T00:00:00Z"
"value": "2014-11-07T00:00:00Z",
"display_timezone": "Europe/Prague"
},
"summary": {
"type": "text",
Expand Down Expand Up @@ -725,7 +729,8 @@
"post_date": {
"type": "date_time",
"name": "Post date",
"value": "2014-11-11T00:00:00Z"
"value": "2014-11-11T00:00:00Z",
"display_timezone": "Europe/Prague"
},
"summary": {
"type": "text",
Expand Down Expand Up @@ -893,7 +898,8 @@
"post_date": {
"type": "date_time",
"name": "Post date",
"value": "2014-10-27T00:00:00Z"
"value": "2014-10-27T00:00:00Z",
"display_timezone": "Europe/Prague"
},
"summary": {
"type": "text",
Expand Down Expand Up @@ -1072,7 +1078,8 @@
"post_date": {
"type": "date_time",
"name": "Post date",
"value": "2014-11-02T00:00:00Z"
"value": "2014-11-02T00:00:00Z",
"display_timezone": "Europe/Prague"
},
"summary": {
"type": "text",
Expand Down Expand Up @@ -1348,7 +1355,8 @@
"post_date": {
"type": "date_time",
"name": "Post date",
"value": "2014-11-07T00:00:00Z"
"value": "2014-11-07T00:00:00Z",
"display_timezone": "Europe/Prague"
},
"summary": {
"type": "text",
Expand Down Expand Up @@ -1519,7 +1527,8 @@
"post_date": {
"type": "date_time",
"name": "Post date",
"value": "2014-11-11T00:00:00Z"
"value": "2014-11-11T00:00:00Z",
"display_timezone": "Europe/Prague"
},
"summary": {
"type": "text",
Expand Down Expand Up @@ -1687,7 +1696,8 @@
"post_date": {
"type": "date_time",
"name": "Post date",
"value": "2014-10-27T00:00:00Z"
"value": "2014-10-27T00:00:00Z",
"display_timezone": "Europe/Prague"
},
"summary": {
"type": "text",
Expand Down
18 changes: 15 additions & 3 deletions Kontent.Ai.Delivery.Caching/Extensions/SerializationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;

namespace Kontent.Ai.Delivery.Caching.Extensions
{
Expand All @@ -14,10 +17,10 @@ public static class SerializationExtensions
/// </summary>
public static JsonSerializerSettings Settings => new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Unspecified,
TypeNameHandling = TypeNameHandling.All, // Allow preserving type information (necessary for deserializing interfaces into implemented types)
ReferenceLoopHandling = ReferenceLoopHandling.Serialize, // The content item models contain recursive references which are supposed to be preserved.
PreserveReferencesHandling = PreserveReferencesHandling.All // The code must not use arrays and readonly collections, otherwise it'll result in "Cannot preserve reference to array or readonly list, or list created from a non-default constructor" exception. (more details at https://stackoverflow.com/a/41307438/1332034)
PreserveReferencesHandling = PreserveReferencesHandling.All, // The code must not use arrays and readonly collections, otherwise it'll result in "Cannot preserve reference to array or readonly list, or list created from a non-default constructor" exception. (more details at https://stackoverflow.com/a/41307438/1332034)
Converters = new List<JsonConverter> { new UtcDateTimeConverter() }
};

/// <summary>
Expand Down Expand Up @@ -52,4 +55,13 @@ public static T FromBson<T>(this byte[] byteArray) where T : class
return serializer.Deserialize<T>(reader);
}
}

internal class UtcDateTimeConverter : IsoDateTimeConverter
{
public UtcDateTimeConverter()
{
DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ";
DateTimeStyles = DateTimeStyles.AdjustToUniversal;
}
}
}
2 changes: 2 additions & 0 deletions Kontent.Ai.Delivery/ContentItems/DateTimes/DateTimeContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ namespace Kontent.Ai.Delivery.ContentItems.DateTimes
{
internal sealed class DateTimeContent : IDateTimeContent
{
[JsonProperty("value")]
public DateTime? Value
{
get; internal set;
}

[JsonProperty("display_timezone")]
public string DisplayTimezone
{
get; internal set;
Expand Down

0 comments on commit c712ba7

Please sign in to comment.