Skip to content

Commit a9e8c60

Browse files
Merge pull request #422 from notion-dotnet/415-make-date-property-value-use-datetimeoffset
Use DateTimeOffset instead of DateTime for DatePropertyValue
2 parents 172570c + f93ceb6 commit a9e8c60

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

Src/Notion.Client/Models/PropertyValue/DatePropertyValue.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Converters;
34

45
namespace Notion.Client
56
{
@@ -26,13 +27,15 @@ public class Date
2627
/// Start date with optional time.
2728
/// </summary>
2829
[JsonProperty("start")]
29-
public DateTime? Start { get; set; }
30+
[JsonConverter(typeof(IsoDateTimeConverter))]
31+
public DateTimeOffset? Start { get; set; }
3032

3133
/// <summary>
3234
/// End date with optional time.
3335
/// </summary>
3436
[JsonProperty("end")]
35-
public DateTime? End { get; set; }
37+
[JsonConverter(typeof(IsoDateTimeConverter))]
38+
public DateTimeOffset? End { get; set; }
3639

3740
/// <summary>
3841
/// Optional time zone information for start and end. Possible values are extracted from the IANA database and they are

Test/Notion.IntegrationTests/PageClientTests.cs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ public async Task Test_UpdatePageProperty_with_date_as_null()
216216
{
217217
Date = new Date
218218
{
219-
Start = Convert.ToDateTime("2020-12-08T12:00:00Z"),
220-
End = Convert.ToDateTime("2025-12-08T12:00:00Z")
219+
Start = DateTimeOffset.Parse("2024-06-26T00:00:00.000+01:00"),
220+
End = DateTimeOffset.Parse("2025-12-08").Date
221221
}
222222
})
223223
.Build();
@@ -236,7 +236,8 @@ public async Task Test_UpdatePageProperty_with_date_as_null()
236236
);
237237

238238
// Assert
239-
setDate?.Date?.Start.Should().Be(Convert.ToDateTime("2020-12-08T12:00:00Z"));
239+
setDate?.Date?.Start.Should().Be(DateTimeOffset.Parse("2024-06-26T00:00:00.000+01:00"));
240+
setDate?.Date?.End.Should().Be(DateTimeOffset.Parse("2025-12-08T00:00:00.000+01:00"));
240241

241242
var pageUpdateParameters = new PagesUpdateParameters
242243
{
@@ -384,4 +385,58 @@ public async Task Bug_exception_when_attempting_to_set_select_property_to_nothin
384385
updatedPage.Properties["Colors1"].As<SelectPropertyValue>().Select.Name.Should().Be("Blue");
385386
updatedPage.Properties["Colors2"].As<SelectPropertyValue>().Select.Should().BeNull();
386387
}
388+
389+
[Fact]
390+
public async Task Verify_date_property_is_parsed_correctly_in_mention_object()
391+
{
392+
var pageRequest = PagesCreateParametersBuilder
393+
.Create(new DatabaseParentInput { DatabaseId = _database.Id })
394+
.AddProperty("Name",
395+
new TitlePropertyValue
396+
{
397+
Title = new List<RichTextBase>
398+
{
399+
new RichTextMention()
400+
{
401+
Mention = new Mention()
402+
{
403+
Page = new ObjectId()
404+
{
405+
Id = _page.Id,
406+
},
407+
Date = new DatePropertyValue()
408+
{
409+
Date = new Date()
410+
{
411+
Start = DateTime.UtcNow
412+
}
413+
}
414+
}
415+
}
416+
}
417+
})
418+
.Build();
419+
420+
var page = await Client.Pages.CreateAsync(pageRequest);
421+
422+
page.Should().NotBeNull();
423+
424+
page.Parent.Should().BeOfType<DatabaseParent>().Which
425+
.DatabaseId.Should().Be(_database.Id);
426+
427+
page.Properties.Should().ContainKey("Name");
428+
var pageProperty = page.Properties["Name"].Should().BeOfType<TitlePropertyValue>().Subject;
429+
430+
var titleProperty = (ListPropertyItem)await Client.Pages.RetrievePagePropertyItemAsync(
431+
new RetrievePropertyItemParameters
432+
{
433+
PageId = page.Id,
434+
PropertyId = pageProperty.Id
435+
});
436+
437+
titleProperty.Results.First()
438+
.As<TitlePropertyItem>()
439+
.Title.As<RichTextMention>()
440+
.Mention.Date.Date.Should().NotBeNull();
441+
}
387442
}

0 commit comments

Comments
 (0)