Skip to content

Commit cb865d3

Browse files
committed
Fix nullable porperties serialization exception in EntryConvert
`EntryConvertGenerator.TransformType()` returns `EntryValueType.Exception` if `propertyType` parameter is nullable primitive type.
1 parent 7ed7501 commit cb865d3

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

src/Moryx/Serialization/EntryConvert/EntryConvert.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@ private static EntryValue CreateEntryValue(PropertyInfo property, ICustomSeriali
111111
isReadOnly = readOnlyAtt?.IsReadOnly ?? false;
112112
}
113113

114+
var type = TransformType(property.PropertyType);
115+
114116
// Prepare object
115117
var entryValue = new EntryValue
116118
{
117-
Type = TransformType(property.PropertyType),
119+
Type = type,
118120
UnitType = customSerialization.GetUnitTypeByAttributes(property),
119121
IsReadOnly = isReadOnly,
120122
Possible = customSerialization.PossibleValues(property.PropertyType, property)
@@ -127,9 +129,9 @@ private static EntryValue CreateEntryValue(PropertyInfo property, ICustomSeriali
127129
else if (entryValue.Possible != null && entryValue.Possible.Length >= 1)
128130
entryValue.Default = entryValue.Possible[0];
129131
else if (property.PropertyType.IsValueType)
130-
entryValue.Default = Activator.CreateInstance(property.PropertyType).ToString();
131-
132-
// Value types should have the default value as current value
132+
entryValue.Default = Activator.CreateInstance(property.PropertyType)?.ToString();
133+
134+
// Value types should have the default value as current value
133135
if (ValueOrStringType(property.PropertyType))
134136
entryValue.Current = ConvertToString(entryValue.Default, customSerialization.FormatProvider);
135137

@@ -313,7 +315,10 @@ public static Entry EncodeObject(object instance, ICustomSerialization customSer
313315
convertedProperty.SubEntries = subEntry.SubEntries;
314316
break;
315317
case EntryValueType.Exception:
316-
convertedProperty.Value.Current = ExceptionPrinter.Print((Exception)value);
318+
if (value is Exception exceptionValue)
319+
{
320+
convertedProperty.Value.Current = ExceptionPrinter.Print(exceptionValue);
321+
}
317322
break;
318323
case EntryValueType.Stream:
319324
var stream = value as Stream;

src/Moryx/Serialization/EntryConvert/EntryConvertGenerator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static partial class EntryConvert
1515
/// <returns>Enum representation of the property type</returns>
1616
public static EntryValueType TransformType(Type propertyType)
1717
{
18-
var valueType = EntryValueType.String;
18+
var valueType = EntryValueType.Exception;
1919
if (propertyType == typeof(Byte))
2020
{
2121
valueType = EntryValueType.Byte;
@@ -56,6 +56,10 @@ public static EntryValueType TransformType(Type propertyType)
5656
{
5757
valueType = EntryValueType.Double;
5858
}
59+
else if (propertyType == typeof(String) || propertyType == typeof(string))
60+
{
61+
valueType = EntryValueType.String;
62+
}
5963
else if (propertyType.IsEnum)
6064
{
6165
valueType = EntryValueType.Enum;

src/Tests/Moryx.Tests/Serialization/DummyClass.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ public enum DummyEnum
5353
ValueA,
5454
ValueB
5555
}
56+
57+
public class NullablePropertiesClass
58+
{
59+
public int? Value { get; set; } = 0;
60+
}
5661
}

src/Tests/Moryx.Tests/Serialization/SerializationTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,16 @@ public void MemoryStreamDecodeReuseCurrentStreamInitSizeIsGreaterThanNewData()
855855
Assert.AreSame(streamInstanceToCheck, targetStreamDummy.MemoryStream);
856856
}
857857

858+
[Test(Description = "Testing nullable properties")]
859+
public void NullableProperty()
860+
{
861+
var nullablePropertiesObject = new NullablePropertiesClass();
862+
863+
var result = EntryConvert.EncodeObject(nullablePropertiesObject);
864+
865+
Assert.AreEqual(EntryValueType.Exception, result.SubEntries[0].Value.Type);
866+
}
867+
858868
[Test(Description = "Encodes a FileStream")]
859869
public void FileStreamEncode()
860870
{

0 commit comments

Comments
 (0)