dotnet / dotnet/aspnetcore

[SignalR] Support union in the js client

Open
#7,298 13 comments 6 reactions 0 assignees View on GitHub
affected-very-few area-signalr enhancement severity-nice-to-have
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

MessagePack for C# supports Union type and I can use it for interfaces. For example, I have the following model that I send to js client:
```
[MessagePackObject]
public class Prices
{
[Key("last")]
public decimal Last { get; set; }

[Key("sell")]
public decimal Sell { get; set; }

[Key("buy")]
public decimal Buy { get; set; }

[Key("product")]
public IProductModel Product { get; set; }
}

[MessagePack.Union(0, typeof(ProductEx0))]
[MessagePack.Union(1, typeof(ProductEx1))]
public interface IProductModel
{
string Name{ get; set; }
}
```
I can create different `IProductModel` and send to the client. I handle creating the corresponding objects manually on the client and all works fine. But, it does't work when I want to deserialize message to a class that contain union. I have the following class on the server side:
```
public RegisterProduct
{
[Key("product")]
public IProductModel Product { get; set; }
}
```

I send the following object from the client:
```
{
product: new ProductEx0()
}
```
MessagePack on server side throws error:

> System.InvalidOperationException: code is invalid. code:129 format:fixmap

I understand that Union types are encoded in different way, but from the client I send simple model. therefore messagepack cannot handle it.

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.