dapr / dapr/dotnet-sdk

Actor response object deserialization with JSON serialization results in empty objects

Open
#1,459 1 comment 0 reactions 0 assignees View on GitHub
area/actor/client discussion
Dominant language
C#
Stars
1.2k
Forks
378
Avg merge
2d 1h
Merged PRs (30d)
6

Description

When using actor proxies with JSON serialization, response objects of actor methods are not deserialized properly out-of-the-box.
I made a small [repro](https://github.com/ngruson/actor-serialization-issue).

A JSON returned by the Dapr runtime could look like this:
```json
{
"value":{
"retVal":{
"id": "166e0bbf-7084-476f-803b-c4e1418eef7e",
"name":"John Doe"}
}
}
```

with a response object defined like this:
```csharp
public record Person(Guid Id, string Name);
```

Since the object properties are lowercased in the JSON and System.Text.Json is case sensitive by default, the properties of the `Person` object below are not populated.

As a workaround, I could pass in `JsonSerializerOptions` when initializing the actor proxy, like so:
```csharp
ActorProxyOptions actorProxyOptions = new()
{
UseJsonSerialization = true,
JsonSerializerOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
}
};

IPersonActor actorProxy = actorProxyFactory.CreateActorProxy(actorId,
nameof(PersonActor), actorProxyOptions);

Person person = await actorProxy.ReturnPerson();
```

It was not immediately apparent to me that I had to set the `PropertyNameCaseInsensitive` property.

Question: should object properties be matched case-insensitively out-of-the-box when using the JSON serializer?
Please add your feedback in the comments.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.