Skip to content

Commit

Permalink
Support MessagePack union types (dotnet#9151)
Browse files Browse the repository at this point in the history
  • Loading branch information
wassim-k authored Oct 2, 2024
1 parent ae262c8 commit 0804e05
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Orleans.Serialization.MessagePack/MessagePackCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void IFieldCodec.WriteField<TBufferWriter>(ref Writer<TBufferWriter> writer, uin
{

var msgPackWriter = new MessagePackWriter(bufferWriter);
MessagePackSerializer.Serialize(expectedType ?? value.GetType(), ref msgPackWriter, value, _options.SerializerOptions);
MessagePackSerializer.Serialize(value.GetType(), ref msgPackWriter, value, _options.SerializerOptions);
msgPackWriter.Flush();

ReferenceCodec.MarkValueField(writer.Session);
Expand Down Expand Up @@ -198,7 +198,7 @@ object IDeepCopier.DeepCopy(object input, CopyContext context)
var sequence = bufferWriter.Value.AsReadOnlySequence();
result = MessagePackSerializer.Deserialize(input.GetType(), sequence, _options.SerializerOptions);
}
catch
finally
{
bufferWriter.Value.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ protected override void Configure(ISerializerBuilder builder)
new() { SubClass = new() { Id = Guid.NewGuid() } },
new() { IntProperty = 150, StringProperty = new string('c', 20), SubClass = new() { Id = Guid.NewGuid() } },
new() { IntProperty = 150_000, StringProperty = new string('c', 6_000), SubClass = new() { Id = Guid.NewGuid() } },
new() { Union = new MyMessagePackUnionVariant1 { IntProperty = 1 } },
new() { Union = new MyMessagePackUnionVariant2 { StringProperty = "String" } },
};

[Fact]
Expand Down Expand Up @@ -59,7 +61,7 @@ public void MessagePackSerializerDeepCopyUntyped()
[Fact]
public void MessagePackSerializerRoundTripThroughCodec()
{
var original = new MyMessagePackClass { IntProperty = 30, StringProperty = "hi", SubClass = new(){ Id = Guid.NewGuid() } };
var original = new MyMessagePackClass { IntProperty = 30, StringProperty = "hi", SubClass = new() { Id = Guid.NewGuid() } };
var result = RoundTripThroughCodec(original);

Assert.Equal(original.IntProperty, result.IntProperty);
Expand All @@ -79,6 +81,29 @@ public void MessagePackSerializerRoundTripThroughUntypedSerializer()
}


[Trait("Category", "BVT")]
public class MessagePackUnionCodecTests : FieldCodecTester<IMyMessagePackUnion?, IFieldCodec<IMyMessagePackUnion?>>
{
public MessagePackUnionCodecTests(ITestOutputHelper output) : base(output)
{
}

protected override void Configure(ISerializerBuilder builder)
{
builder.AddMessagePackSerializer();
}

protected override IMyMessagePackUnion? CreateValue() => new MyMessagePackUnionVariant1() { IntProperty = 30 };

protected override IMyMessagePackUnion?[] TestValues => new IMyMessagePackUnion?[]
{
null,
new MyMessagePackUnionVariant1 { IntProperty = 1 },
new MyMessagePackUnionVariant2 { StringProperty = "String" },
};
}


[Trait("Category", "BVT")]
public class MessagePackCodecCopierTests : CopierTester<MyMessagePackClass?, IDeepCopier<MyMessagePackClass?>>
{
Expand Down
27 changes: 24 additions & 3 deletions test/Orleans.Serialization.UnitTests/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using MessagePack;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -909,14 +908,14 @@ public sealed class MyCombinedForeignLibraryValueTypeSurrogateConverter :
IConverter<MySecondForeignLibraryType, MySecondForeignLibraryTypeSurrogate>
{
public MyFirstForeignLibraryType ConvertFromSurrogate(in MyFirstForeignLibraryTypeSurrogate surrogate)
=> new () { Num = surrogate.Num, String = surrogate.String, DateTimeOffset = surrogate.DateTimeOffset };
=> new() { Num = surrogate.Num, String = surrogate.String, DateTimeOffset = surrogate.DateTimeOffset };
public MyFirstForeignLibraryTypeSurrogate ConvertToSurrogate(in MyFirstForeignLibraryType value)
=> new() { Num = value.Num, String = value.String, DateTimeOffset = value.DateTimeOffset };

public MySecondForeignLibraryType ConvertFromSurrogate(in MySecondForeignLibraryTypeSurrogate surrogate)
=> new() { Name = surrogate.Name, Value = surrogate.Value, Timestamp = surrogate.Timestamp };
public MySecondForeignLibraryTypeSurrogate ConvertToSurrogate(in MySecondForeignLibraryType value)
=> new () { Name = value.Name, Value = value.Value, Timestamp = value.Timestamp };
=> new() { Name = value.Name, Value = value.Value, Timestamp = value.Timestamp };
}

[MessagePackObject]
Expand All @@ -930,6 +929,9 @@ public sealed record MyMessagePackClass

[Key(2)]
public MyMessagePackSubClass SubClass { get; init; }

[Key(3)]
public IMyMessagePackUnion Union { get; init; }
}

[MessagePackObject]
Expand All @@ -939,4 +941,23 @@ public sealed record MyMessagePackSubClass
public Guid Id { get; init; }
}

[Union(0, typeof(MyMessagePackUnionVariant1))]
[Union(1, typeof(MyMessagePackUnionVariant2))]
public interface IMyMessagePackUnion
{
}

[MessagePackObject]
public sealed record MyMessagePackUnionVariant1 : IMyMessagePackUnion
{
[Key(0)]
public int IntProperty { get; init; }
}

[MessagePackObject]
public sealed record MyMessagePackUnionVariant2 : IMyMessagePackUnion
{
[Key(0)]
public string StringProperty { get; init; }
}
}

0 comments on commit 0804e05

Please sign in to comment.