diff --git a/Octokit.Tests/SimpleJsonSerializerTests.cs b/Octokit.Tests/SimpleJsonSerializerTests.cs index ddfe21dd0f..18756d5cc0 100644 --- a/Octokit.Tests/SimpleJsonSerializerTests.cs +++ b/Octokit.Tests/SimpleJsonSerializerTests.cs @@ -80,8 +80,16 @@ public void HandlesBase64EncodedStrings() } } + public class TheDeserializeMethod { + [Fact] + public void DeserializesEventInfosWithUnderscoresInName() + { + const string json = "{\"event\":\"head_ref_deleted\"}"; + new SimpleJsonSerializer().Deserialize(json); + } + [Fact] public void UnderstandsRubyCasing() { diff --git a/Octokit/Http/SimpleJsonSerializer.cs b/Octokit/Http/SimpleJsonSerializer.cs index c8f93ea28e..e0eb8524f1 100644 --- a/Octokit/Http/SimpleJsonSerializer.cs +++ b/Octokit/Http/SimpleJsonSerializer.cs @@ -87,10 +87,11 @@ public override object DeserializeObject(object value, Type type) var stringValue = value as string; if (stringValue != null) { + stringValue = stringValue.Replace("-", ""); if (ReflectionUtils.GetTypeInfo(type).IsEnum) { // remove '-' from values coming in to be able to enum utf-8 - stringValue = stringValue.Replace("-", ""); + stringValue = RemoveHyphenAndUnderscore(stringValue); return Enum.Parse(type, stringValue, ignoreCase: true); } @@ -99,6 +100,7 @@ public override object DeserializeObject(object value, Type type) var underlyingType = Nullable.GetUnderlyingType(type); if (ReflectionUtils.GetTypeInfo(underlyingType).IsEnum) { + stringValue = RemoveHyphenAndUnderscore(stringValue); return Enum.Parse(underlyingType, stringValue, ignoreCase: true); } } @@ -118,6 +120,15 @@ public override object DeserializeObject(object value, Type type) 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> SetterValueFactory(Type type) { return type.GetPropertiesAndFields()