Add StringEnum to handle unknown enum values returned from API (#1595)

* Added StringEnum<TEnum>

* Added tests

* Make sure the serializer can work with StringEnum

* Use StringEnum for EventInfo.Event

* Add convention test to assert that all Response models use StringEnum<> to wrap enum properties

* Add Stringnum<> to all response types failing convention test

* Handle StringEnum to Enum conversion when Issue response model populates IssueUpdate request model

* Fix unit test

* Refactor SimpleJsonSerializer to expose the DeserializeEnum strategy so it can be used in StringEnum class

* Need to expose/use SerializeEnum functionality too, so we use the correct string representation of enum values that have custom properties (eg ReactionType Plus1 to "+1")

* fix unit tests, since the string is now the "correct" upstream api value

* Add a couple of tests for the Enum serialize/deserialize when underscores, hyphens and custom property attributes are present

* Compare parsed values for equality

* add convention test to ensure enum members all have Parameter property set

* update test to cover implicit conversions too

* this test should work but fails at the moment due to magic hyphen removal in deserializer causing a one way trip from utf-8 to EncodingType.Utf8 with no way to get back

* (unsuccesfully) expand event info test to try to catch more cases of unknown event types

* fix broken integration test while im here

* Fixed build errors after .NET Core merge

* Value -> StringValue, ParsedValue -> Value

* Don't allow StringValue to be null

* Ignore enums not used in request/response models

* Added ParameterAttribute to almost all enum values

* Ignore Language enum

* Fix failing tests

* Fix milestone sort parameter and tests

* whitespace

* fix milestone unit tests

* Fix StringEnum.Equals ... This could've been embarrassing!

* Change SimpleJsonSerializer Enum handling to only use `[Parameter()]` attributes (no more magic removal of hyphen/underscores from strings)

* Tidy up this integration test while im here

* Only test request/response enums in convention test

* Keep skipping Language

* Remove unused method

* Remove excluded enum types

* Removed unnecessary ParameterAttributes

* Remove unused enum

* Add StringEnum test for string-comparison of two invalid values

* Bring back IssueCommentSort and use it in IssueCommentRequest

This reverts commit 38a4a291d1476ef8c992fe0f76956974b6f32a49.

* Use assembly instead of namespace for Octokit check

* Add failing test to reproduce the issue where only the first enum paramter/value was added to the cache

* Fix deserializer enum cache to include all enum members rather than only the first member encountered

* Use a static SimpleJsonSerializer in StringEnum

* Remove serializer instance in StringEnum

* Add some documentation on StringEnum<TEnum>

* Fix parameter value to resolve failing integration test
This commit is contained in:
Kristian Hellang
2017-06-25 11:29:57 +02:00
committed by Ryan Gribble
parent 8877eea1cb
commit 5ee4d64046
54 changed files with 897 additions and 112 deletions
+68 -44
View File
@@ -9,7 +9,7 @@ namespace Octokit.Internal
{
public class SimpleJsonSerializer : IJsonSerializer
{
readonly GitHubSerializerStrategy _serializationStrategy = new GitHubSerializerStrategy();
static readonly GitHubSerializerStrategy _serializationStrategy = new GitHubSerializerStrategy();
public string Serialize(object item)
{
@@ -21,6 +21,16 @@ namespace Octokit.Internal
return SimpleJson.DeserializeObject<T>(json, _serializationStrategy);
}
internal static string SerializeEnum(Enum value)
{
return _serializationStrategy.SerializeEnumHelper(value).ToString();
}
internal static object DeserializeEnum(string value, Type type)
{
return _serializationStrategy.DeserializeEnumHelper(value, type);
}
class GitHubSerializerStrategy : PocoJsonSerializerStrategy
{
readonly List<string> _membersWhichShouldPublishNull = new List<string>();
@@ -75,6 +85,11 @@ namespace Octokit.Internal
return true;
}
internal object SerializeEnumHelper(Enum p)
{
return SerializeEnum(p);
}
[SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase",
Justification = "The API expects lowercase values")]
protected override object SerializeEnum(Enum p)
@@ -82,6 +97,43 @@ namespace Octokit.Internal
return p.ToParameter();
}
internal object DeserializeEnumHelper(string value, Type type)
{
if (!_cachedEnums.ContainsKey(type))
{
//First add type to Dictionary
_cachedEnums.Add(type, new Dictionary<object, object>());
//then try to get all custom attributes, this happens only once per type
var fields = type.GetRuntimeFields();
foreach (var field in fields)
{
if (field.Name == "value__")
continue;
var attribute = (ParameterAttribute)field.GetCustomAttribute(typeof(ParameterAttribute));
if (attribute != null)
{
if (!_cachedEnums[type].ContainsKey(attribute.Value))
{
var fieldValue = field.GetValue(null);
_cachedEnums[type].Add(attribute.Value, fieldValue);
}
}
}
}
if (_cachedEnums[type].ContainsKey(value))
{
return _cachedEnums[type][value];
}
else
{
//dictionary does not contain enum value and has no custom attribute. So add it for future loops and return value
var parsed = Enum.Parse(type, value, ignoreCase: true);
_cachedEnums[type].Add(value, parsed);
return parsed;
}
}
private string _type;
// Overridden to handle enums.
@@ -92,39 +144,11 @@ namespace Octokit.Internal
if (stringValue != null)
{
if (ReflectionUtils.GetTypeInfo(type).IsEnum)
var typeInfo = ReflectionUtils.GetTypeInfo(type);
if (typeInfo.IsEnum)
{
if (!_cachedEnums.ContainsKey(type))
{
//First add type to Dictionary
_cachedEnums.Add(type, new Dictionary<object, object>());
//then try to get all custom attributes, this happens only once per type
var fields = type.GetRuntimeFields();
foreach (var field in fields)
{
if (field.Name == "value__")
continue;
var attribute = (ParameterAttribute)field.GetCustomAttribute(typeof(ParameterAttribute));
if (attribute != null)
{
if (attribute.Value.Equals(value))
_cachedEnums[type].Add(attribute.Value, field.GetValue(null));
}
}
}
if (_cachedEnums[type].ContainsKey(value))
{
return _cachedEnums[type][value];
}
else
{
//dictionary does not contain enum value and has no custom attribute. So add it for future loops and return value
// remove '-' from values coming in to be able to enum utf-8
stringValue = RemoveHyphenAndUnderscore(stringValue);
var parsed = Enum.Parse(type, stringValue, ignoreCase: true);
_cachedEnums[type].Add(value, parsed);
return parsed;
}
return DeserializeEnumHelper(stringValue, type);
}
if (ReflectionUtils.IsNullableType(type))
@@ -132,8 +156,7 @@ namespace Octokit.Internal
var underlyingType = Nullable.GetUnderlyingType(type);
if (ReflectionUtils.GetTypeInfo(underlyingType).IsEnum)
{
stringValue = RemoveHyphenAndUnderscore(stringValue);
return Enum.Parse(underlyingType, stringValue, ignoreCase: true);
return DeserializeEnumHelper(stringValue, underlyingType);
}
}
@@ -147,6 +170,16 @@ namespace Octokit.Internal
return stringValue.Split(',');
}
}
if (typeInfo.IsGenericType)
{
var typeDefinition = typeInfo.GetGenericTypeDefinition();
if (typeof(StringEnum<>).IsAssignableFrom(typeDefinition))
{
return Activator.CreateInstance(type, stringValue);
}
}
}
else if (jsonValue != null)
{
@@ -165,15 +198,6 @@ namespace Octokit.Internal
return base.DeserializeObject(value, type);
}
static string RemoveHyphenAndUnderscore(string stringValue)
{
// remove '-' from values coming in to be able to enum utf-8
stringValue = stringValue.Replace("-", "");
// remove '-' from values coming in to be able to enum EventInfoState names with underscores in them. Like "head_ref_deleted"
stringValue = stringValue.Replace("_", "");
return stringValue;
}
internal override IDictionary<string, KeyValuePair<Type, ReflectionUtils.SetDelegate>> SetterValueFactory(Type type)
{
return type.GetPropertiesAndFields()