diff --git a/YamlDotNet.Analyzers.StaticGenerator/SerializableSyntaxReceiver.cs b/YamlDotNet.Analyzers.StaticGenerator/SerializableSyntaxReceiver.cs index f1548bb13..3ec376371 100644 --- a/YamlDotNet.Analyzers.StaticGenerator/SerializableSyntaxReceiver.cs +++ b/YamlDotNet.Analyzers.StaticGenerator/SerializableSyntaxReceiver.cs @@ -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)); } diff --git a/YamlDotNet.Test/Analyzers/StaticGenerator/ObjectTests.cs b/YamlDotNet.Test/Analyzers/StaticGenerator/ObjectTests.cs index 0a5e04256..9128cf62c 100644 --- a/YamlDotNet.Test/Analyzers/StaticGenerator/ObjectTests.cs +++ b/YamlDotNet.Test/Analyzers/StaticGenerator/ObjectTests.cs @@ -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(yaml); + Assert.Equal(EnumMemberedEnum.EmptyValue, actual); + } + + [Fact] + public void EnumDeserializedUsesEnumNameWhenMemberIsNull() + { + var deserializer = new StaticDeserializerBuilder(new StaticContext()).Build(); + var yaml = "NullValue"; + var actual = deserializer.Deserialize(yaml); + Assert.Equal(EnumMemberedEnum.NullValue, actual); + } + [Fact] public void EnumSerializationUsesEnumMemberAttribute() { @@ -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] diff --git a/YamlDotNet.Test/Serialization/DeserializerTest.cs b/YamlDotNet.Test/Serialization/DeserializerTest.cs index 178376d02..618a4b86c 100644 --- a/YamlDotNet.Test/Serialization/DeserializerTest.cs +++ b/YamlDotNet.Test/Serialization/DeserializerTest.cs @@ -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(yaml); + Assert.Equal(EnumMemberedEnum.EmptyValue, actual); + } + + [Fact] + public void EnumDeserializedUsesEnumNameWhenMemberIsNull() + { + var deserializer = new DeserializerBuilder().Build(); + var yaml = "NullValue"; + var actual = deserializer.Deserialize(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 diff --git a/YamlDotNet.Test/Serialization/SerializationTests.cs b/YamlDotNet.Test/Serialization/SerializationTests.cs index 4f73dba89..3b83d5a9c 100644 --- a/YamlDotNet.Test/Serialization/SerializationTests.cs +++ b/YamlDotNet.Test/Serialization/SerializationTests.cs @@ -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 diff --git a/YamlDotNet/Serialization/TypeInspectors/ReflectionTypeInspector.cs b/YamlDotNet/Serialization/TypeInspectors/ReflectionTypeInspector.cs index 946712543..00273f8f1 100644 --- a/YamlDotNet/Serialization/TypeInspectors/ReflectionTypeInspector.cs +++ b/YamlDotNet/Serialization/TypeInspectors/ReflectionTypeInspector.cs @@ -59,7 +59,7 @@ public override string GetEnumValue(object enumValue) if (enumMembers.Length > 0) { var attribute = enumMembers[0].GetCustomAttribute(); - if (attribute != null) + if (!string.IsNullOrWhiteSpace(attribute?.Value)) { result = attribute.Value; }