Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible loss of DateTime's Kind with DotNetclient #367

Open
zelenij opened this issue Jul 11, 2024 · 5 comments
Open

Possible loss of DateTime's Kind with DotNetclient #367

zelenij opened this issue Jul 11, 2024 · 5 comments

Comments

@zelenij
Copy link

zelenij commented Jul 11, 2024

I'm trying to upgrade my code to everything latest, .net8 and all fable related stuff, including Fable.Remoting. In one of my tests I'm checking if a certain server call returns a UTC Kind in a DateTime. It fails, because Kind is Local. This used to work with an older version of the library. I've checked the server side code and I can see that Kind is indeed UTC. Could it be that the value is lost in serialisation/deserialisation?

@Zaid-Ajaj
Copy link
Owner

Could it be that the value is lost in serialisation/deserialisation?

Hi there @zelenij
Yes, it is possible that Kind is lost when sent across the wire. However, for the longest time I can remember, we recommend using DateTimeOffset if you care about if you care about UTC vs. Local which will maintain its offset when sent/received from either server or client.

Hope this helps!

@zelenij
Copy link
Author

zelenij commented Jul 12, 2024

I saw the advise regarding DateTimeOffset. Unfortunately, DateTime is all over my codebase, and changing it would be a huge pain. All my times are in UTC, so DateTimeOffset is not strictly necessary.

@Zaid-Ajaj
Copy link
Owner

@zelenij Alright, let's find out why this happens. Since you are using the dotnet client, it means that both client and server use Fable.Remoting.Json to handle all JSON-related things. In my unit test for a universal DateTime, I see this:

testCase "Union with DateTime conversion" <| fun () ->
    let dateInput = DateTime.Now.ToUniversalTime()
    let serialized = serialize (UnionWithDateTime.Date dateInput)
    let deserialized = deserialize<UnionWithDateTime> serialized
    match deserialized with
    | Int _ -> fail()
    | Date dateOutput ->
        Expect.equal dateInput.Second dateOutput.Second "Seconds are the same"
        Expect.equal dateInput.Minute dateOutput.Minute "Minutes are the same"
        Expect.equal dateInput.Hour dateOutput.Hour "Hours are the same"
        Expect.equal dateInput.Day dateOutput.Day "Days are the same"
        Expect.equal dateInput.Month dateOutput.Month "Months are the same"
        Expect.equal dateInput.Year dateOutput.Year "Year are the same"
        Expect.equal dateInput.Kind dateOutput.Kind "Kinds are the same"

Which shows that the Kind is maintained when serialized then deserialized to JSON.

In the dotnet client, we do use specific settings where we set DateParseHandling to None which should be fine actually, since the default behaviour is lossy:

/// Parses a JSON iput string to a .NET type using Fable JSON converter
let parseAs<'t> (json: string) =
    let options = JsonSerializerSettings()
    options.Converters.Add converter
    options.DateParseHandling <- DateParseHandling.None
    JsonConvert.DeserializeObject<'t>(json, options)

Any chance you could provide an example of a DateTime roundtrip that reproduces the issue?

@zelenij
Copy link
Author

zelenij commented Jul 14, 2024

Thanks, I'll do more debugging on my side to see if I can clearly rule out any other option and/or create a small standalone test to reproduce the problem

@zelenij
Copy link
Author

zelenij commented Jul 16, 2024

So far I can see that by server side code prepares a DateTime with Utc Kind. However, when it arrives serialised through the HTTP request via the DotNetClient, it looks like this:

"2024-07-16T15:57:10.9807420Z"

If I read the deserialiser code correctly, it then calls DateTime.Parse on this string. And the result has Local Kind:

image

Maybe the Json serialisation is at fault here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants