Skip to content

Commit cce1dd7

Browse files
committed
Fix nullable porperties serialization exception in EntryConvert
Creating an instance of the nullable primitive using the underlying type.
1 parent 7ed7501 commit cce1dd7

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

src/Moryx/Serialization/EntryConvert/EntryConvert.cs

Lines changed: 15 additions & 6 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)
@@ -126,10 +128,14 @@ private static EntryValue CreateEntryValue(PropertyInfo property, ICustomSeriali
126128
entryValue.Default = defaultAttribute.Value.ToString();
127129
else if (entryValue.Possible != null && entryValue.Possible.Length >= 1)
128130
entryValue.Default = entryValue.Possible[0];
129-
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
131+
else if (property.PropertyType.IsValueType)
132+
{
133+
var underlyingType = Nullable.GetUnderlyingType(property.PropertyType);
134+
entryValue.Default = underlyingType != null
135+
? Activator.CreateInstance(underlyingType).ToString()
136+
: Activator.CreateInstance(property.PropertyType).ToString();
137+
}
138+
// Value types should have the default value as current value
133139
if (ValueOrStringType(property.PropertyType))
134140
entryValue.Current = ConvertToString(entryValue.Default, customSerialization.FormatProvider);
135141

@@ -313,7 +319,10 @@ public static Entry EncodeObject(object instance, ICustomSerialization customSer
313319
convertedProperty.SubEntries = subEntry.SubEntries;
314320
break;
315321
case EntryValueType.Exception:
316-
convertedProperty.Value.Current = ExceptionPrinter.Print((Exception)value);
322+
//if (value is Exception exceptionValue)
323+
//{
324+
convertedProperty.Value.Current = ExceptionPrinter.Print((Exception)value);
325+
//}
317326
break;
318327
case EntryValueType.Stream:
319328
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
@@ -55,7 +55,11 @@ public static EntryValueType TransformType(Type propertyType)
5555
else if (propertyType == typeof(Double))
5656
{
5757
valueType = EntryValueType.Double;
58-
}
58+
}
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,14 @@ 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+
Assert.DoesNotThrow(() => EntryConvert.EncodeObject(nullablePropertiesObject));
864+
}
865+
858866
[Test(Description = "Encodes a FileStream")]
859867
public void FileStreamEncode()
860868
{

0 commit comments

Comments
 (0)