Skip to content

Commit

Permalink
Merge pull request #1037 from AArnott/fix1033
Browse files Browse the repository at this point in the history
Fix formatting of default `ImmutableArray<T>` values
  • Loading branch information
AArnott authored Sep 14, 2020
2 parents 5920a07 + 71f78d5 commit 4a93481
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
18 changes: 12 additions & 6 deletions src/MessagePack.ImmutableCollection/Formatters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ public class ImmutableArrayFormatter<T> : IMessagePackFormatter<ImmutableArray<T
{
public void Serialize(ref MessagePackWriter writer, ImmutableArray<T> value, MessagePackSerializerOptions options)
{
if (value == null)
if (value.IsDefault)
{
writer.WriteNil();
}
else if (value.IsEmpty)
{
writer.WriteArrayHeader(0);
}
else
{
IMessagePackFormatter<T> formatter = options.Resolver.GetFormatterWithVerify<T>();

writer.WriteArrayHeader(value.Length);

foreach (T item in value)
{
formatter.Serialize(ref writer, item, options);
Expand All @@ -36,14 +39,17 @@ public ImmutableArray<T> Deserialize(ref MessagePackReader reader, MessagePackSe
{
if (reader.TryReadNil())
{
return ImmutableArray<T>.Empty;
return default;
}
else
{
IMessagePackFormatter<T> formatter = options.Resolver.GetFormatterWithVerify<T>();

var len = reader.ReadArrayHeader();
if (len == 0)
{
return ImmutableArray<T>.Empty;
}

IMessagePackFormatter<T> formatter = options.Resolver.GetFormatterWithVerify<T>();
ImmutableArray<T>.Builder builder = ImmutableArray.CreateBuilder<T>(len);
options.Security.DepthStep(ref reader);
try
Expand All @@ -58,7 +64,7 @@ public ImmutableArray<T> Deserialize(ref MessagePackReader reader, MessagePackSe
reader.Depth--;
}

return builder.ToImmutable();
return builder.MoveToImmutable();
}
}
}
Expand Down
37 changes: 30 additions & 7 deletions tests/MessagePack.Tests/ExtensionTests/ImmutableCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,38 @@ public void InterfaceCollectionTest()
}

[Fact]
public void ImmutableArrayTest()
public void ImmutableArray_WithContent()
{
var a = ImmutableArray.CreateRange(new[] { 1, 10, 100 });
ImmutableArray<int>? b = ImmutableArray.CreateRange(new[] { 1, 10, 100 });
ImmutableArray<int>? c = null;
ImmutableArray<int> populated = ImmutableArray.CreateRange(new[] { 1, 10, 100 });
this.Convert(populated).Is(1, 10, 100);
}

this.Convert(a).Is(1, 10, 100);
this.Convert(b).Is(1, 10, 100);
this.Convert(c).IsNull();
[Fact]
public void ImmutableArray_Nullable_WithContent()
{
ImmutableArray<int>? populatedNullable = ImmutableArray.CreateRange(new[] { 1, 10, 100 });
this.Convert(populatedNullable).Is(1, 10, 100);
}

[Fact]
public void ImmutableArray_Nullable_Null()
{
ImmutableArray<int>? nullNullable = null;
Assert.Null(this.Convert(nullNullable));
}

[Fact]
public void ImmutableArray_Empty()
{
ImmutableArray<int> defaultArray = ImmutableArray<int>.Empty;
Assert.True(this.Convert(defaultArray).IsEmpty);
}

[Fact]
public void ImmutableArray_Default()
{
ImmutableArray<int> defaultArray = default;
Assert.True(this.Convert(defaultArray).IsDefault);
}
}
}

0 comments on commit 4a93481

Please sign in to comment.