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

Update EnumSchema.cs to handle Enums with duplicate values #166

Merged
merged 3 commits into from
Sep 18, 2024
Merged
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
5 changes: 4 additions & 1 deletion src/AvroConvert/AvroObjectServices/Schemas/EnumSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
int v = Convert.ToInt32(values.GetValue(i), CultureInfo.InvariantCulture);
this.avroToCSharpValueMapping.Add(Convert.ToInt64(values.GetValue(i), CultureInfo.InvariantCulture));
this.symbolToValue.Add(this.symbols[i], v);
this.valueToSymbol.Add(v, this.symbols[i]);
this.valueToSymbol.TryAdd(v, this.symbols[i]);

Check failure on line 86 in src/AvroConvert/AvroObjectServices/Schemas/EnumSchema.cs

View workflow job for this annotation

GitHub Actions / build

'Dictionary<int, string>' does not contain a definition for 'TryAdd' and no accessible extension method 'TryAdd' accepting a first argument of type 'Dictionary<int, string>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 86 in src/AvroConvert/AvroObjectServices/Schemas/EnumSchema.cs

View workflow job for this annotation

GitHub Actions / build

'Dictionary<int, string>' does not contain a definition for 'TryAdd' and no accessible extension method 'TryAdd' accepting a first argument of type 'Dictionary<int, string>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 86 in src/AvroConvert/AvroObjectServices/Schemas/EnumSchema.cs

View workflow job for this annotation

GitHub Actions / build

'Dictionary<int, string>' does not contain a definition for 'TryAdd' and no accessible extension method 'TryAdd' accepting a first argument of type 'Dictionary<int, string>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 86 in src/AvroConvert/AvroObjectServices/Schemas/EnumSchema.cs

View workflow job for this annotation

GitHub Actions / build

'Dictionary<int, string>' does not contain a definition for 'TryAdd' and no accessible extension method 'TryAdd' accepting a first argument of type 'Dictionary<int, string>' could be found (are you missing a using directive or an assembly reference?)
}
}
}
Expand All @@ -92,6 +92,9 @@

internal bool TryGetSymbolValue(string symbol, out int value) =>
this.symbolToValue.TryGetValue(symbol, out value);

internal int GetSymbolPosition(string symbol) =>
this.symbols.IndexOf(symbol);

internal string GetSymbolByValue(int value)
{
Expand Down
6 changes: 3 additions & 3 deletions src/AvroConvert/AvroObjectServices/Write/Resolvers/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ internal Encoder.WriteItem ResolveEnum(EnumSchema schema)
{
value = EnumParser.GetEnumName(enumType, value.ToString());
}

if (!schema.TryGetSymbolValue(value.ToString(), out var symbolValue))
var position = schema.GetSymbolPosition(value.ToString());
if (position < 0)
{
throw new AvroTypeException(
$"[Enum] Provided value is not of the enum [{schema.Name}] members");
}

e.WriteEnum(symbolValue);
e.WriteEnum(position);
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,44 @@ public void Class_with_enum_with_member_value_attributes(Func<object, Type, dyna
Assert.Equal(TestEnumWithMembers.Negative, deserialized.EnumPropWithDefault);
Assert.Equal(TestEnumWithMembers.Negative, deserialized.EnumPropWithStringDefault);
}

[Theory]
[MemberData(nameof(TestEngine.All), MemberType = typeof(TestEngine))]
public void Enum_with_duplicate_values(Func<object, Type, dynamic> engine)
{
var itemsToTest =
new [] {
TestDuplicateEnum.Test,
TestDuplicateEnum.Exam, //Compilation already sets these values to the primary duplicate value.
TestDuplicateEnum.Error,
TestDuplicateEnum.Fail,
TestDuplicateEnum.TestLabel,
TestDuplicateEnum.TestLabel2
};

foreach (var toSerialize in itemsToTest)
{
//Act
var deserialized = engine.Invoke(toSerialize, typeof(TestDuplicateEnum));

//Assert
Assert.Equal((int)toSerialize, (int)deserialized);
}
}

[Theory]
[MemberData(nameof(TestEngine.All), MemberType = typeof(TestEngine))]
public void Class_with_enum_with_duplicate_value(Func<object, Type, dynamic> engine)
{
//Arrange
ClassWithDuplicateEnums toSerialize = _fixture.Create<ClassWithDuplicateEnums>();

//Act
var deserialized = engine.Invoke(toSerialize, typeof(ClassWithDuplicateEnums));

//Assert
Assert.NotNull(deserialized);
Assert.Equal(toSerialize, deserialized);
}
}
}
19 changes: 19 additions & 0 deletions tests/AvroConvertTests/TestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,25 @@ public class ClassWithEnum
public TestEnum? SecondEnumProp { get; set; }
}

public enum TestDuplicateEnum {
Test = 0,
Exam = Test,
Fail = 1,
Error = Fail,
TestLabel = 2,
TestLabel2 = 2
}

[Equals(DoNotAddEqualityOperators = true)]
public class ClassWithDuplicateEnums {
[DefaultValue(TestDuplicateEnum.Fail)]
public TestDuplicateEnum? EnumProp { get; set; }
[DefaultValue("Error")]
public TestDuplicateEnum? EnumProp2 { get; set;}
[DefaultValue(2)]
public TestDuplicateEnum? EnumProp3 { get; set; }
}

public class ClassWithEnumDefiningMembers
{
public TestEnumWithMembers? EnumProp { get; set; }
Expand Down
Loading