Skip to content

Commit

Permalink
#600 Fix nullable enum roundtrip
Browse files Browse the repository at this point in the history
+semver:fix
  • Loading branch information
aaubry authored Apr 9, 2021
2 parents 381ffec + a36b671 commit fedcbea
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 63 deletions.
21 changes: 16 additions & 5 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,25 @@ public void DeserializeScalarLongBase60Number()
result.Should().Be(77744246530L);
}

[Fact]
public void RoundtripEnums()
[Theory]
[InlineData(EnumExample.One)]
[InlineData(EnumExample.One | EnumExample.Two)]
public void RoundtripEnums(EnumExample value)
{
var flags = EnumExample.One | EnumExample.Two;
var result = DoRoundtripFromObjectTo<EnumExample>(value);

result.Should().Be(value);
}

var result = DoRoundtripFromObjectTo<EnumExample>(flags);
[Theory]
[InlineData(EnumExample.One)]
[InlineData(EnumExample.One | EnumExample.Two)]
[InlineData(null)]
public void RoundtripNullableEnums(EnumExample? value)
{
var result = DoRoundtripFromObjectTo<EnumExample?>(value);

result.Should().Be(flags);
result.Should().Be(value);
}

[Fact]
Expand Down
117 changes: 59 additions & 58 deletions YamlDotNet/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,68 +42,69 @@ bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func<IPars
return false;
}

if (expectedType.IsEnum())
// Strip off the nullable type, if present
var underlyingType = Nullable.GetUnderlyingType(expectedType) ?? expectedType;

if (underlyingType.IsEnum())
{
value = Enum.Parse(expectedType, scalar.Value, true);
value = Enum.Parse(underlyingType, scalar.Value, true);
return true;
}
else
{
var underlyingType = Nullable.GetUnderlyingType(expectedType);
var typeCode = underlyingType != null ? underlyingType.GetTypeCode() : expectedType.GetTypeCode();
switch (typeCode)
{
case TypeCode.Boolean:
value = DeserializeBooleanHelper(scalar.Value);
break;

case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
value = DeserializeIntegerHelper(typeCode, scalar.Value);
break;

case TypeCode.Single:
value = float.Parse(scalar.Value, YamlFormatter.NumberFormat);
break;

case TypeCode.Double:
value = double.Parse(scalar.Value, YamlFormatter.NumberFormat);
break;

case TypeCode.Decimal:
value = decimal.Parse(scalar.Value, YamlFormatter.NumberFormat);
break;

case TypeCode.String:
var typeCode = underlyingType.GetTypeCode();
switch (typeCode)
{
case TypeCode.Boolean:
value = DeserializeBooleanHelper(scalar.Value);
break;

case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
value = DeserializeIntegerHelper(typeCode, scalar.Value);
break;

case TypeCode.Single:
value = float.Parse(scalar.Value, YamlFormatter.NumberFormat);
break;

case TypeCode.Double:
value = double.Parse(scalar.Value, YamlFormatter.NumberFormat);
break;

case TypeCode.Decimal:
value = decimal.Parse(scalar.Value, YamlFormatter.NumberFormat);
break;

case TypeCode.String:
value = scalar.Value;
break;

case TypeCode.Char:
value = scalar.Value[0];
break;

case TypeCode.DateTime:
// TODO: This is probably incorrect. Use the correct regular expression.
value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture);
break;

default:
if (expectedType == typeof(object))
{
// Default to string
value = scalar.Value;
break;

case TypeCode.Char:
value = scalar.Value[0];
break;

case TypeCode.DateTime:
// TODO: This is probably incorrect. Use the correct regular expression.
value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture);
break;

default:
if (expectedType == typeof(object))
{
// Default to string
value = scalar.Value;
}
else
{
value = TypeConverter.ChangeType(scalar.Value, expectedType);
}
break;
}
}
else
{
value = TypeConverter.ChangeType(scalar.Value, expectedType);
}
break;
}
return true;
}
Expand Down

0 comments on commit fedcbea

Please sign in to comment.