Skip to content

Commit a263ab6

Browse files
authored
Sort order of Serializer.GetProto<FooEnum>() as reported in #1102 (#1123)
* very minor spelling update * Issue 1102 - added IComparable to EnumMember and a directive to Sort the array within GetEnumValues of MetaType. * Issue 1102 - added extra compare logic * Issue 1102 - Removing conditional compilation so that GetEnumValues returns an array sorted by enum value * Issue 1102 - adjusting sort logic to order from zero to int.MaxValue then from negative int.MaxValue to -1. EnumMembers that have a value outside of int min max ranges return zero. This change impacts sort order for proto2. * Issue 1102 - todo comment clean up * Issue 1102 - Added test case for an enum that has negatives and out of range values. Adjusted sort logic to give correct order. Note that out of range values are treated as equivalent no matter the value being compared to, this impacts the context only proto generation. For example, not positions of B and D public enum OutOfRangeEnum : long { A = 1, B = -4, C = int.MaxValue, D = ((long)int.MaxValue) + 1, E = -int.MaxValue, F = ((long)int.MinValue) - 1, } /* for context only enum OutOfRangeEnum { ZERO = 0; // proto3 requires a zero value as the first item (it can be named anything) A = 1; C = 2147483647; B = -4; // D = 2147483648; // note: enums should be valid 32-bit integers E = -2147483647; // F = -2147483649; // note: enums should be valid 32-bit integers } */ * Issue 1102 - Correction to sort order so that negatives are from -int.MaxValue to -1. This changes where out of range values appear, for example, note positions of B, D and F in the context only output; public enum OutOfRangeEnum : long { A = 1, B = -4, C = int.MaxValue, D = ((long)int.MaxValue) + 1, E = -int.MaxValue, F = ((long)int.MinValue) - 1, } /* for context only enum OutOfRangeEnum { ZERO = 0; // proto3 requires a zero value as the first item (it can be named anything) A = 1; C = 2147483647; E = -2147483647; B = -4; // D = 2147483648; // note: enums should be valid 32-bit integers // F = -2147483649; // note: enums should be valid 32-bit integers } */
1 parent af0fcc6 commit a263ab6

5 files changed

Lines changed: 216 additions & 8 deletions

File tree

‎docs/contract_first.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ change the version/contents from the copy baked into the tool. Normally, standar
2929

3030
## Additional options
3131

32-
Additional configuration options can be specified at attributes against each `<AdditionalFiles>` node, to fine tune your options; this is very similar to the options available with the command-line tools:
32+
Additional configuration options can be specified as attributes against each `<AdditionalFiles>` node, to fine tune your options; this is very similar to the options available with the command-line tools:
3333

3434
- `ImportPaths` - specifies a comma delimited list of additional import locations; usually this isn't required, as schemas are resolved relative to each file; this is resolved relative to the current file, and
3535
can indicate "upwards" locations like `../..` - but the actual lookup happens *purely* within the virtual file system of `<AdditionalFiles>` nodes - it does not allow external file access

‎src/Examples/EnumTests.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ message EnumFoo {
6969
}
7070
enum blah {
7171
B = 0;
72-
A = -1;
7372
C = 1;
73+
A = -1;
7474
}
7575
", proto, ignoreLineEndingDifferences: true);
7676
}
@@ -93,8 +93,8 @@ message NonNullValues {
9393
}
9494
enum blah {
9595
B = 0;
96-
A = -1;
9796
C = 1;
97+
A = -1;
9898
}
9999
", proto, ignoreLineEndingDifferences: true);
100100
}
@@ -127,8 +127,8 @@ enum OutOfRangeEnum {
127127
A = 1;
128128
B = 4;
129129
C = 2147483647;
130-
// D = 2147483648; // note: enums should be valid 32-bit integers
131130
E = -2147483647;
131+
// D = 2147483648; // note: enums should be valid 32-bit integers
132132
// F = -2147483649; // note: enums should be valid 32-bit integers
133133
}
134134
*/
@@ -178,8 +178,8 @@ message NullValues {
178178
}
179179
enum blah {
180180
B = 0;
181-
A = -1;
182181
C = 1;
182+
A = -1;
183183
}
184184
", proto, ignoreLineEndingDifferences: true);
185185
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
using ProtoBuf.Meta;
2+
using System.Reflection;
3+
using Xunit;
4+
5+
namespace ProtoBuf.Test.Issues
6+
{
7+
public class Issue1102
8+
{
9+
10+
[Fact]
11+
public void TestEnumOrderWithNegativesAndOutOfRangeValues()
12+
{
13+
var model = RuntimeTypeModel.Create();
14+
model.UseImplicitZeroDefaults = false;
15+
16+
string proto = model.GetSchema(typeof(HazOutOfRangeAndNegatives), ProtoSyntax.Proto3);
17+
18+
Assert.Equal(@"syntax = ""proto3"";
19+
package ProtoBuf.Test.Issues;
20+
21+
message HazOutOfRangeAndNegatives {
22+
int64 OutOfRange = 1; // declared as invalid enum: OutOfRangeEnum
23+
InRangeEnum InRange = 2;
24+
}
25+
enum InRangeEnum {
26+
ZERO = 0; // proto3 requires a zero value as the first item (it can be named anything)
27+
A = 1;
28+
C = 2147483647;
29+
E = -2147483647;
30+
B = -4;
31+
}
32+
/* for context only
33+
enum OutOfRangeEnum {
34+
ZERO = 0; // proto3 requires a zero value as the first item (it can be named anything)
35+
A = 1;
36+
C = 2147483647;
37+
E = -2147483647;
38+
B = -4;
39+
// D = 2147483648; // note: enums should be valid 32-bit integers
40+
// F = -2147483649; // note: enums should be valid 32-bit integers
41+
}
42+
*/
43+
", proto, ignoreLineEndingDifferences: true);
44+
}
45+
46+
public enum InRangeEnum : long
47+
{
48+
A = 1,
49+
B = -4,
50+
C = int.MaxValue,
51+
E = -int.MaxValue,
52+
}
53+
54+
public enum OutOfRangeEnum : long
55+
{
56+
A = 1,
57+
B = -4,
58+
C = int.MaxValue,
59+
D = ((long)int.MaxValue) + 1,
60+
E = -int.MaxValue,
61+
F = ((long)int.MinValue) - 1,
62+
}
63+
64+
[ProtoContract]
65+
public class HazOutOfRangeAndNegatives
66+
{
67+
[ProtoMember(1)]
68+
public OutOfRangeEnum OutOfRange { get; set; }
69+
70+
[ProtoMember(2)]
71+
public InRangeEnum InRange { get; set; }
72+
}
73+
74+
75+
[Fact]
76+
public void TestEnumOrderInProtoMatchesDefinitionOrder()
77+
{
78+
var letters = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
79+
80+
MemberInfo[] foundList = typeof(FooEnum).GetMembers(BindingFlags.Public | BindingFlags.Static);
81+
82+
Assert.Equal(letters.Length, foundList.Length);
83+
84+
for (var i = 0; i < foundList.Length; i++)
85+
{
86+
var field = (FieldInfo)foundList[i];
87+
Assert.Equal(letters[i], field.Name);
88+
}
89+
90+
var proto = Serializer.GetProto<FooEnum>();
91+
92+
Assert.Equal(ExpectedOutput, proto, ignoreLineEndingDifferences: true);
93+
}
94+
95+
private static string ExpectedOutput = @"syntax = ""proto3"";
96+
package ProtoBuf.Test.Issues;
97+
98+
enum FooEnum {
99+
A = 0;
100+
B = 1;
101+
C = 2;
102+
D = 3;
103+
E = 4;
104+
F = 5;
105+
G = 6;
106+
H = 7;
107+
I = 8;
108+
J = 9;
109+
K = 10;
110+
L = 11;
111+
M = 12;
112+
N = 13;
113+
O = 14;
114+
P = 15;
115+
Q = 16;
116+
R = 17;
117+
S = 18;
118+
T = 19;
119+
U = 20;
120+
V = 21;
121+
W = 22;
122+
X = 23;
123+
Y = 24;
124+
Z = 25;
125+
}
126+
";
127+
128+
[ProtoContract]
129+
public enum FooEnum
130+
{
131+
[ProtoEnum]
132+
A,
133+
[ProtoEnum]
134+
B,
135+
[ProtoEnum]
136+
C,
137+
[ProtoEnum]
138+
D,
139+
[ProtoEnum]
140+
E,
141+
[ProtoEnum]
142+
F,
143+
[ProtoEnum]
144+
G,
145+
[ProtoEnum]
146+
H,
147+
[ProtoEnum]
148+
I,
149+
[ProtoEnum]
150+
J,
151+
[ProtoEnum]
152+
K,
153+
[ProtoEnum]
154+
L,
155+
[ProtoEnum]
156+
M,
157+
[ProtoEnum]
158+
N,
159+
[ProtoEnum]
160+
O,
161+
[ProtoEnum]
162+
P,
163+
[ProtoEnum]
164+
Q,
165+
[ProtoEnum]
166+
R,
167+
[ProtoEnum]
168+
S,
169+
[ProtoEnum]
170+
T,
171+
[ProtoEnum]
172+
U,
173+
[ProtoEnum]
174+
V,
175+
[ProtoEnum]
176+
W,
177+
[ProtoEnum]
178+
X,
179+
[ProtoEnum]
180+
Y,
181+
[ProtoEnum]
182+
Z
183+
}
184+
}
185+
}

‎src/protobuf-net/Meta/EnumMember.cs‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace ProtoBuf.Meta
88
/// Describes a named constant integer, i.e. an enum value
99
/// </summary>
1010
[StructLayout(LayoutKind.Auto)]
11-
public readonly struct EnumMember : IEquatable<EnumMember>
11+
public readonly struct EnumMember : IEquatable<EnumMember>, IComparable<EnumMember>
1212
{
1313
/// <summary>
1414
/// Gets the declared name of this enum member
@@ -119,5 +119,26 @@ internal void Validate()
119119
if (string.IsNullOrWhiteSpace(Name))
120120
ThrowHelper.ThrowInvalidOperationException("All enum declarations must have valid names");
121121
}
122+
123+
public int CompareTo(EnumMember other)
124+
{
125+
var thisValue = TryGetInt32();
126+
var otherValue = other.TryGetInt32();
127+
128+
if (!thisValue.HasValue && !otherValue.HasValue) return 0;
129+
if (!thisValue.HasValue) return 1;
130+
if (!otherValue.HasValue) return -1;
131+
132+
if (thisValue.Value < 0 && otherValue.Value >= 0)
133+
{
134+
return 1;
135+
}
136+
if (otherValue.Value < 0 && thisValue.Value >= 0)
137+
{
138+
return -1;
139+
}
140+
141+
return thisValue.Value.CompareTo(otherValue.Value);
142+
}
122143
}
123144
}

‎src/protobuf-net/Meta/MetaType.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,10 @@ public ValueMember[] GetFields()
17201720
public EnumMember[] GetEnumValues()
17211721
{
17221722
if (!HasEnums) return Array.Empty<EnumMember>();
1723-
return Enums.ToArray();
1723+
1724+
var arr = Enums.ToArray();
1725+
Array.Sort(arr);
1726+
return arr;
17241727
}
17251728

17261729
/// <summary>
@@ -2027,7 +2030,6 @@ internal void WriteSchema(HashSet<Type> callstack, StringBuilder builder, int in
20272030
{
20282031
var enums = GetEnumValues();
20292032

2030-
20312033
bool allValid = IsValidEnum(enums);
20322034
if (!allValid) NewLine(builder, indent).Append("/* for context only");
20332035
NewLine(builder, indent).Append("enum ").Append(GetSchemaTypeName(callstack)).Append(" {");

0 commit comments

Comments
 (0)