Skip to content

Commit aa89ba9

Browse files
MilosMilos
Milos
authored and
Milos
committed
DatetimeOffset input - fix automatic modification to local time
1 parent 3308aee commit aa89ba9

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/Examples/JsonApiDotNetCoreExample/Models/TodoItem.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using JsonApiDotNetCore.Models;
33

44
namespace JsonApiDotNetCoreExample.Models
@@ -25,12 +25,12 @@ public TodoItem()
2525
[Attr("achieved-date", isFilterable: false, isSortable: false)]
2626
public DateTime? AchievedDate { get; set; }
2727

28-
2928
[Attr("updated-date")]
3029
public DateTime? UpdatedDate { get; set; }
3130

32-
33-
31+
[Attr("offset-date")]
32+
public DateTimeOffset? OffsetDate { get; set; }
33+
3434
public int? OwnerId { get; set; }
3535
public int? AssigneeId { get; set; }
3636
public Guid? CollectionId { get; set; }

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Reflection;
56
using JsonApiDotNetCore.Extensions;
@@ -36,8 +37,12 @@ public object Deserialize(string requestBody)
3637
{
3738
try
3839
{
39-
var bodyJToken = JToken.Parse(requestBody);
40-
40+
JToken bodyJToken;
41+
using (JsonReader jsonReader = new JsonTextReader(new StringReader(requestBody)))
42+
{
43+
jsonReader.DateParseHandling = DateParseHandling.None;
44+
bodyJToken = JToken.Load(jsonReader);
45+
}
4146
if (RequestIsOperation(bodyJToken))
4247
{
4348
_jsonApiContext.IsBulkOperationRequest = true;

test/JsonApiDotNetCoreExampleTests/Acceptance/TodoItemsControllerTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ public async Task Can_Post_TodoItem()
404404
_context.SaveChanges();
405405

406406
var todoItem = _todoItemFaker.Generate();
407+
var nowOffset = new DateTimeOffset();
407408
var content = new
408409
{
409410
data = new
@@ -413,7 +414,8 @@ public async Task Can_Post_TodoItem()
413414
{
414415
{ "description", todoItem.Description },
415416
{ "ordinal", todoItem.Ordinal },
416-
{ "created-date", todoItem.CreatedDate }
417+
{ "created-date", todoItem.CreatedDate },
418+
{ "offset-date", nowOffset }
417419
},
418420
relationships = new
419421
{
@@ -446,6 +448,7 @@ public async Task Can_Post_TodoItem()
446448
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
447449
Assert.Equal(todoItem.Description, deserializedBody.Description);
448450
Assert.Equal(todoItem.CreatedDate.ToString("G"), deserializedBody.CreatedDate.ToString("G"));
451+
Assert.Equal(nowOffset.ToString("yyyy-MM-ddTHH:mm:ssK"), deserializedBody.OffsetDate?.ToString("yyyy-MM-ddTHH:mm:ssK"));
449452
Assert.Null(deserializedBody.AchievedDate);
450453
}
451454

0 commit comments

Comments
 (0)