Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Moryx/Serialization/EntryConvert/EntryConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ private static EntryValue CreateEntryValue(PropertyInfo property, ICustomSeriali
entryValue.Default = defaultAttribute.Value.ToString();
else if (entryValue.Possible != null && entryValue.Possible.Length >= 1)
entryValue.Default = entryValue.Possible[0];
else if (property.PropertyType.IsValueType)
entryValue.Default = Activator.CreateInstance(property.PropertyType).ToString();
else if (property.PropertyType.IsValueType)
{
var underlyingType = Nullable.GetUnderlyingType(property.PropertyType);
entryValue.Default = underlyingType != null
? Activator.CreateInstance(underlyingType).ToString()
: Activator.CreateInstance(property.PropertyType).ToString();
}

// Value types should have the default value as current value
if (ValueOrStringType(property.PropertyType))
Expand Down
5 changes: 5 additions & 0 deletions src/Tests/Moryx.Tests/Serialization/DummyClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public enum DummyEnum
ValueA,
ValueB
}

public class NullablePropertiesClass
{
public int? Value { get; set; } = 0;
}
}
8 changes: 8 additions & 0 deletions src/Tests/Moryx.Tests/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,14 @@ public void MemoryStreamEncode()
Assert.AreEqual("VGhpcyBpcyBhIHRlc3Q=", entry.SubEntries[0].Value.Current);
}

[Test(Description = "Testing nullable properties")]
public void NullableProperty()
{
var nullablePropertiesObject = new NullablePropertiesClass();

Assert.DoesNotThrow(() => EntryConvert.EncodeObject(nullablePropertiesObject));
}

[Test(Description = "Decodes to a MemoryStream and creates a new stream")]
public void MemoryStreamDecode()
{
Expand Down