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

MessagePackFormatter fails to deserialize Union types #460

Closed
AArnott opened this issue May 2, 2020 · 1 comment · Fixed by #490
Closed

MessagePackFormatter fails to deserialize Union types #460

AArnott opened this issue May 2, 2020 · 1 comment · Fixed by #490
Assignees

Comments

@AArnott
Copy link
Member

AArnott commented May 2, 2020

Given a type hierarchy that utilizes MessagePack's UnionAttribute:

[MessagePackObject]
[Union(0, typeof(UnionDerivedClass))]
public abstract class UnionBaseType
{
}

[MessagePackObject]
public class UnionDerivedClass : UnionBaseType
{
}

And given a server method that declares to return the base type:

public UnionBaseType ReturnUnionType() => new UnionDerivedClass();

MessagePackFormatter fails to deserialize the result:

UnionBaseType result = await this.clientRpc.InvokeWithCancellationAsync<UnionBaseType>(nameof(MessagePackServer.ReturnUnionType), null, this.TimeoutToken);
Assert.IsType<UnionDerivedClass>(result);

This throws on the Invoke line because the union data was not serialized. MessagePackFormatter serializes the return value as UnionDerivedClass directly, so no union data appears necessary. But on the receiving end, only UnionBaseClass is expected, so union data is necessary.

To fix this, the MessagePackFormatter needs to know what the declared type is and tell the MessagePackSerializer to serialize that as a generic type argument. That way the union data (if any) will be included.

@AArnott AArnott added the bug label May 2, 2020
@AArnott AArnott self-assigned this May 2, 2020
@AArnott
Copy link
Member Author

AArnott commented May 2, 2020

We need to support this for arguments as well as return values.
While return types have a declared type on both sides, arguments do not have a declared type on the originating side -- only the receiving side declares parameter types.
This leaves us with a very interesting problem for how to send arguments involving Union types with the MessagePackFormatter (or even the JsonMessageFormatter if combined with a custom discriminator JsonConverter, perhaps).

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

Successfully merging a pull request may close this issue.

1 participant