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

Fix list serialization of complex class with Dictionary field. #19

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Microsoft.Avro.Core/Serializers/EnumerableSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ protected override Expression BuildSerializerSafe(Expression encoder, Expression
Expression.Assign(buffer, Expression.New(listType)),
Expression.Assign(counter, Expression.Constant(0)),
Expression.Assign(enumerator, Expression.Call(value, getEnumerator)),
Expression.Assign(chunkCounter, Expression.Constant(0)),
Expression.Loop(
Expression.IfThenElse(
Expression.NotEqual(Expression.Call(enumerator, moveNext), Expression.Constant(false)),
Expand Down
10 changes: 10 additions & 0 deletions Microsoft.Avro.Tests/AvroSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ public void Serializer_SerializeComplexNestedClass()
RoundTripSerializationWithCheck(ComplexNestedClass.Create());
}

[Fact]
public void Serializer_SerializeListOfComplexNestedClass()
{
RoundTripSerializationWithCheck(new List<ComplexNestedClass>
{
ComplexNestedClass.Create(),
ComplexNestedClass.Create()
});
}

[Fact]
public void Serializer_SerializeGuidClass()
{
Expand Down
12 changes: 11 additions & 1 deletion Microsoft.Avro.Tests/TestClasses/TestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ internal class ComplexNestedClass : IEquatable<ComplexNestedClass>
[DataMember]
public Recursive RecursiveField { get; set; }

[ProtoMember(4)]
[DataMember]
public Dictionary<int, string> DictionaryField { get; set; }

public static Random R = new Random(typeof(ComplexNestedClass).GetHashCode());

public static ComplexNestedClass Create()
Expand All @@ -581,7 +585,8 @@ public static ComplexNestedClass Create()
{
NestedField = SimpleFlatClass.Create(),
IntNestedField = R.Next(),
RecursiveField = Recursive.Create()
RecursiveField = Recursive.Create(),
DictionaryField = new Dictionary<int, string> {[R.Next()] = $"val:{R.Next()}"}
};
}

Expand All @@ -602,6 +607,11 @@ public bool Equals(ComplexNestedClass other)
return false;
}

if (this.DictionaryField.Count != other.DictionaryField.Count || this.DictionaryField.Except(other.DictionaryField).Any())
{
return false;
}

return this.RecursiveField.Equals(other.RecursiveField);
}

Expand Down