Deserializer should handle nullable StringEnum<T> (#1760)

* add deserializer tests for nullable enums, StringEnum and nullable StringEnum properties

* Fix deserializing nullable structs by using the underlying type when nullable

* StringEnum<T> should behave like an enum, returning default(T) when it is uninitialised/null/blank

* Don't allow null to be passed into StringEnum ctor - if it needs to be null then it should be declared as nullable

* fix expected json

* move logic to determine if property is a StringEnum<T> into helper function

* serializer needs to treat StringEnum<T> specially by serializing the enum value according to existing serializer strategy (parameter attributes and so on)

* remove fallback to default(T)

* add test to assert ctor throws exception when null passed in

* remove test for default(T) fallback behaviour

* Fix exception in serializer test - StringEnum property must be initialized otherwise an exception is thrown when attempting to serialize

* Dont allow empty strings either
This commit is contained in:
Ryan Gribble
2018-02-16 20:12:44 +10:00
committed by GitHub
parent b35b60f24c
commit 41b4059c11
5 changed files with 151 additions and 41 deletions
+3 -7
View File
@@ -29,7 +29,9 @@ namespace Octokit
public StringEnum(string stringValue)
{
_stringValue = stringValue ?? string.Empty;
Ensure.ArgumentNotNullOrEmptyString(stringValue, nameof(stringValue));
_stringValue = stringValue;
_parsedValue = null;
}
@@ -66,12 +68,6 @@ namespace Octokit
return true;
}
if (string.IsNullOrEmpty(StringValue))
{
value = default(TEnum);
return false;
}
try
{
// Use the SimpleJsonSerializer to parse the string to Enum according to the GitHub Api strategy