Skip to content

Commit

Permalink
Fix code generation for serializers with auto generated property iden…
Browse files Browse the repository at this point in the history
…tifiers (#8586)
  • Loading branch information
ReubenBond authored Aug 30, 2023
1 parent 610a673 commit 36829a8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/Orleans.CodeGenerator/SerializerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private static MemberDeclarationSyntax GenerateSerializeMethod(
VariableDeclaration(
PredefinedType(Token(SyntaxKind.UIntKeyword)),
SingletonSeparatedList(VariableDeclarator(previousFieldIdVar.Identifier)
.WithInitializer(EqualsValueClause(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))))))));
.WithInitializer(EqualsValueClause(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0U))))))));
}

if (type.IncludePrimaryConstructorParameters)
Expand Down Expand Up @@ -495,7 +495,7 @@ private static MemberDeclarationSyntax GenerateDeserializeMethod(
body.Add(LocalDeclarationStatement(
VariableDeclaration(
PredefinedType(Token(SyntaxKind.UIntKeyword)),
SingletonSeparatedList(VariableDeclarator(idVar.Identifier, null, EqualsValueClause(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))))))));
SingletonSeparatedList(VariableDeclarator(idVar.Identifier, null, EqualsValueClause(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0U))))))));
}

// C#: Field header = default;
Expand All @@ -512,7 +512,7 @@ private static MemberDeclarationSyntax GenerateDeserializeMethod(
body.Add(GetDeserializerLoop(constructorParameterMembers));
if (members.Count > 0)
{
body.Add(ExpressionStatement(AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, idVar, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)))));
body.Add(ExpressionStatement(AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, idVar, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0U)))));
}

body.Add(IfStatement(headerVar.Member("IsEndBaseFields"), GetDeserializerLoop(nonCtorMembers)));
Expand Down Expand Up @@ -619,7 +619,7 @@ StatementSyntax GetDeserializerLoop(List<ISerializableMember> members)
}

// C#: if (id == <fieldId>) { ... }
var ifStatement = IfStatement(BinaryExpression(SyntaxKind.EqualsExpression, idVar, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)description.FieldId))),
var ifStatement = IfStatement(BinaryExpression(SyntaxKind.EqualsExpression, idVar, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(description.FieldId))),
ifBody);

loopBody.Add(ifStatement);
Expand Down
23 changes: 22 additions & 1 deletion test/Orleans.Serialization.UnitTests/GeneratedSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Collections.Generic;
using System.IO.Pipelines;
using Xunit;
using System.Net.NetworkInformation;
using System.Linq;

namespace Orleans.Serialization.UnitTests;
Expand Down Expand Up @@ -64,6 +63,28 @@ public void GeneratedRecordSerializersRoundTripThroughCodec()
Assert.Equal(original.StarSign, result.StarSign);
}

[Fact]
public void AutoGeneratedPublicPropertyIdsTest()
{
var r = new Random();
var original = new PocoWithAutogeneratedIds
{
A = r.Next(),
B = r.Next(),
C = r.Next(),
D = r.Next(),
E = r.Next(),
F = r.Next(),
G = r.Next(),
H = r.Next(),
I = r.Next(),
J = r.Next(),
K = r.Next(),
};
var result = RoundTripThroughCodec(original);
Assert.Equal(original, result);
}

[Fact]
public void RecursiveTypeSerializersRoundTripThroughSerializer()
{
Expand Down
50 changes: 50 additions & 0 deletions test/Orleans.Serialization.UnitTests/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,56 @@ public class SomeClassWithSerializers
public override string ToString() => $"{nameof(IntField)}: {IntField}, {nameof(IntProperty)}: {IntProperty}";
}

[GenerateSerializer(GenerateFieldIds = GenerateFieldIds.PublicProperties)]
public class PocoWithAutogeneratedIds : IEquatable<PocoWithAutogeneratedIds>
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
public int D { get; set; }
public int E { get; set; }
public int F { get; set; }
public int G { get; set; }
public int H { get; set; }
public int I { get; set; }
public int J { get; set; }
public int K { get; set; }

public override bool Equals(object obj) => Equals(obj as PocoWithAutogeneratedIds);
public bool Equals(PocoWithAutogeneratedIds other) => other is not null
&& A == other.A
&& B == other.B
&& C == other.C
&& D == other.D
&& E == other.E
&& F == other.F
&& G == other.G
&& H == other.H
&& I == other.I
&& J == other.J
&& K == other.K;

public override int GetHashCode()
{
var hash = new HashCode();
hash.Add(A);
hash.Add(B);
hash.Add(C);
hash.Add(D);
hash.Add(E);
hash.Add(F);
hash.Add(G);
hash.Add(H);
hash.Add(I);
hash.Add(J);
hash.Add(K);
return hash.ToHashCode();
}

public static bool operator ==(PocoWithAutogeneratedIds left, PocoWithAutogeneratedIds right) => EqualityComparer<PocoWithAutogeneratedIds>.Default.Equals(left, right);
public static bool operator !=(PocoWithAutogeneratedIds left, PocoWithAutogeneratedIds right) => !(left == right);
}

[GenerateSerializer]
[Alias("sercla1")]
public class SerializableClassWithCompiledBase : List<int>
Expand Down

0 comments on commit 36829a8

Please sign in to comment.