dapr / dapr/dotnet-sdk

Adding Custom Json Converters to actor serialization options doesn't seem to work

Open
#891 1 comment 2 reactions 0 assignees View on GitHub
kind/bug
Dominant language
C#
Stars
1.2k
Forks
378
Avg merge
2d 1h
Merged PRs (30d)
6

Description

I add custom serializers for DateOnly and TimeOnly

```
type DateOnlyConverter() =
inherit JsonConverter()
override __.Read(reader: byref, _, _) = DateOnly.Parse(reader.GetString())
override __.Write(writer: Utf8JsonWriter, value: DateOnly, _) = writer.WriteStringValue(value.ToString("yyyy-MM-dd"))

type TimeOnlyConverter() =
inherit JsonConverter()
override __.Read(reader: byref, _, _) = TimeOnly.Parse(reader.GetString())
override __.Write(writer: Utf8JsonWriter, value: TimeOnly, _) = writer.WriteStringValue(value.ToString("HH:mm:ss.fffffff"))
```

```
let jsonConverters = [ JsonFSharpConverter() :> JsonConverter; DateOnlyConverter(); TimeOnlyConverter() ]
let addConvertersTo (converters: IList) = jsonConverters |> List.iter (fun converter -> converters.Add(converter))

let jsonSerializerOptions =
let res = JsonSerializerOptions()
addConvertersTo res.Converters
res

builder.Services
.AddControllers()
.AddJsonOptions(fun jsonOptions -> addConvertersTo jsonOptions.JsonSerializerOptions.Converters)
.AddDapr(fun daprClientBuider -> daprClientBuider.UseJsonSerializationOptions(jsonSerializerOptions) |> ignore)

builder.Services.AddActors(fun actorRuntimeOptions -> addConvertersTo actorRuntimeOptions.JsonSerializerOptions.Converters)
builder.Services.AddDaprClient()
```

Now I need to call an Actor method with a parameter of type DateOnly

```
//Actor
member __.GetChart(firstSession:DateOnly) =
...

//Caller
let d = DateOnly.Parse("2022-06-01")
let res = LiveActor.Proxy("GCA@500").GetChart(d)
```

## Expected Behavior
The actor method should receive the correct value 2022-06-01 for the parameter
## Actual Behavior

The actor method receives zero (0001-01-01). The converter is never called

## Note
The issue only applies to actors. The dapr client successfully adopts the added custom json converters

Contributor guide

Open the contributing guide

Research direction

Start at the AddActors configuration and actorRuntimeOptions.JsonSerializerOptions.Converters path, then reproduce the GetChart(DateOnly) call shown in the issue. Compare that actor serialization path with the working AddDaprClient configuration; done means the custom DateOnlyConverter is invoked and the actor receives 2022-06-01 instead of 0001-01-01.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, fsharp
Domain
distributed-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.