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

MessagePack Unions not serialized correctly. #48

Closed
PhilJollans opened this issue Jan 16, 2024 · 2 comments
Closed

MessagePack Unions not serialized correctly. #48

PhilJollans opened this issue Jan 16, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@PhilJollans
Copy link

Describe the bug

I have several classes defined as a Union, using the MessagePack.Union attribute.
See https://github.com/MessagePack-CSharp/MessagePack-CSharp?tab=readme-ov-file#union

I had no problem with .NET 6, but with .NET 8 I find that the Union classes are not serialized correctly,

So far as I can tell, it is necessary to provide a type parameter to MessagePackSerializer.Serialize(), specifying the base class used for the union.

I have defined my own formatter class

  public class MessagePackFormatterT<T1> : FormatterBase
    where T1 : class
  {
    protected override byte[] SerializeInternal( object obj )
    {
      return MessagePackSerializer.Serialize<T1> ( obj as T1 );
    }

    protected override T DeserializeInternal<T>( byte[] bytes )
    {
      return MessagePackSerializer.Deserialize<T> ( bytes );
    }
  }

which I specify with both PipeClient and the PipeSever

_client = new PipeClient<MyBase> ( PipeName, formatter: new MessagePackFormatterT<MyBase>() ) ;
_server = new PipeServer<MyBase> ( PipeName, new MessagePackFormatterT<MyBase>() );

In my application, this seems to have fixed the problem, but I'm not sure that it is a general solution.

Steps to reproduce the bug

Define a set of classes using MessageBase.Union, following the example in
https://github.com/MessagePack-CSharp/MessagePack-CSharp?tab=readme-ov-file#union

I defined the base class as an abstract class, not an interface.

Create a PipeServer an a PipeClient based on the union class.

Create an instance of one of the derived classes.

Send it via the PipeServer.

On receiving the message, the PipeClient will generate an error.

Expected behavior

No response

Screenshots

No response

NuGet package version

No response

Platform

Console

IDE

Visual Studio 2022

Additional context

.NET 8

@PhilJollans PhilJollans added the bug Something isn't working label Jan 16, 2024
@HavenDV
Copy link
Owner

HavenDV commented Jan 20, 2024

Added your implementation to H.Formatters.MessagePack lib.
a34a0a2

@HavenDV
Copy link
Owner

HavenDV commented Jan 20, 2024

This is available in the latest prerelease version and will be stable after some time
I don't see any other way to solve the problem, so I'm closing this.

@HavenDV HavenDV closed this as completed Jan 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants