Skip to content

Commit

Permalink
Merge pull request #967 from EdwardCooke/ec-966-emptyenummember
Browse files Browse the repository at this point in the history
Dont use enummember attribute when it's null or empty

+semver:fix
  • Loading branch information
EdwardCooke authored Sep 3, 2024
2 parents 06b67d9 + 9c50cbd commit 440d607
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ private void HandleEnum(ITypeSymbol type)
if (enumMember != null)
{
var argument = enumMember.NamedArguments.FirstOrDefault(x => x.Key == "Value");
memberValue = (string)argument.Value.Value!;
if (!string.IsNullOrWhiteSpace(argument.Value.Value as string))
{
memberValue = (string)argument.Value.Value!;
}
}
mappings.Add(new EnumMappings(type, memberName, memberValue));
}
Expand Down
42 changes: 41 additions & 1 deletion YamlDotNet.Test/Analyzers/StaticGenerator/ObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ public void EnumDeserializationUsesEnumMemberAttribute()
Assert.Equal(EnumMemberedEnum.Hello, actual);
}

[Fact]
public void EnumDeserializedUsesEnumNameWhenMemberIsEmpty()
{
var deserializer = new StaticDeserializerBuilder(new StaticContext()).Build();
var yaml = "EmptyValue";
var actual = deserializer.Deserialize<EnumMemberedEnum>(yaml);
Assert.Equal(EnumMemberedEnum.EmptyValue, actual);
}

[Fact]
public void EnumDeserializedUsesEnumNameWhenMemberIsNull()
{
var deserializer = new StaticDeserializerBuilder(new StaticContext()).Build();
var yaml = "NullValue";
var actual = deserializer.Deserialize<EnumMemberedEnum>(yaml);
Assert.Equal(EnumMemberedEnum.NullValue, actual);
}

[Fact]
public void EnumSerializationUsesEnumMemberAttribute()
{
Expand All @@ -209,13 +227,35 @@ public void EnumSerializationUsesEnumMemberAttribute()
Assert.Equal("goodbye", actual.TrimNewLines());
}

[Fact]
public void EnumSerializationUsesEnumMemberAttributeWithEmptyValue()
{
var serializer = new StaticSerializerBuilder(new StaticContext()).Build();
var actual = serializer.Serialize(EnumMemberedEnum.EmptyValue);
Assert.Equal("EmptyValue", actual.TrimNewLines());
}

[Fact]
public void EnumSerializationUsesEnumMemberAttributeWithNullValue()
{
var serializer = new StaticSerializerBuilder(new StaticContext()).Build();
var actual = serializer.Serialize(EnumMemberedEnum.NullValue);
Assert.Equal("NullValue", actual.TrimNewLines());
}

[YamlSerializable]
public enum EnumMemberedEnum
{
No = 0,

[System.Runtime.Serialization.EnumMember(Value = "goodbye")]
Hello = 1
Hello = 1,

[System.Runtime.Serialization.EnumMember(Value = "")]
EmptyValue = 2,

[System.Runtime.Serialization.EnumMember()]
NullValue = 3
}
#endif
[Fact]
Expand Down
26 changes: 25 additions & 1 deletion YamlDotNet.Test/Serialization/DeserializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,36 @@ public void EnumDeserializationUsesEnumMemberAttribute()
Assert.Equal(EnumMemberedEnum.Hello, actual);
}

[Fact]
public void EnumDeserializedUsesEnumNameWhenMemberIsEmpty()
{
var deserializer = new DeserializerBuilder().Build();
var yaml = "EmptyValue";
var actual = deserializer.Deserialize<EnumMemberedEnum>(yaml);
Assert.Equal(EnumMemberedEnum.EmptyValue, actual);
}

[Fact]
public void EnumDeserializedUsesEnumNameWhenMemberIsNull()
{
var deserializer = new DeserializerBuilder().Build();
var yaml = "NullValue";
var actual = deserializer.Deserialize<EnumMemberedEnum>(yaml);
Assert.Equal(EnumMemberedEnum.NullValue, actual);
}

public enum EnumMemberedEnum
{
No = 0,

[System.Runtime.Serialization.EnumMember(Value = "goodbye")]
Hello = 1
Hello = 1,

[System.Runtime.Serialization.EnumMember(Value = "")]
EmptyValue = 2,

[System.Runtime.Serialization.EnumMember()]
NullValue = 3

}
#endif
Expand Down
24 changes: 23 additions & 1 deletion YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,10 +1498,32 @@ public void EnumSerializationUsesEnumMemberAttribute()
Assert.Equal("goodbye", actual.TrimNewLines());
}

[Fact]
public void EnumSerializationUsesEnumMemberAttributeWithEmptyValue()
{
var serializer = new SerializerBuilder().Build();
var actual = serializer.Serialize(EnumMemberedEnum.EmptyValue);
Assert.Equal("EmptyValue", actual.TrimNewLines());
}

[Fact]
public void EnumSerializationUsesEnumMemberAttributeWithNullValue()
{
var serializer = new SerializerBuilder().Build();
var actual = serializer.Serialize(EnumMemberedEnum.NullValue);
Assert.Equal("NullValue", actual.TrimNewLines());
}

public enum EnumMemberedEnum
{
[System.Runtime.Serialization.EnumMember(Value = "goodbye")]
Hello = 1
Hello = 1,

[System.Runtime.Serialization.EnumMember(Value = "")]
EmptyValue = 2,

[System.Runtime.Serialization.EnumMember()]
NullValue = 3
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override string GetEnumValue(object enumValue)
if (enumMembers.Length > 0)
{
var attribute = enumMembers[0].GetCustomAttribute<EnumMemberAttribute>();
if (attribute != null)
if (!string.IsNullOrWhiteSpace(attribute?.Value))
{
result = attribute.Value;
}
Expand Down

0 comments on commit 440d607

Please sign in to comment.