From 015ecb97dea9c5d8a145c2fc355a427bc87947bb Mon Sep 17 00:00:00 2001 From: Jan Ove Skogheim Date: Wed, 25 Feb 2015 20:32:26 +0100 Subject: [PATCH 1/2] Revert https://github.com/janovesk/octokit.net/commit/4d2bc143b305e65d59ed8d8404f6e1b1e5858dbb. The parameter attribute only works for serialization, not deserialization. --- Octokit/Models/Response/EventInfo.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Octokit/Models/Response/EventInfo.cs b/Octokit/Models/Response/EventInfo.cs index 949e059c62..2c61881d86 100644 --- a/Octokit/Models/Response/EventInfo.cs +++ b/Octokit/Models/Response/EventInfo.cs @@ -156,13 +156,11 @@ public enum EventInfoState /// /// The pull request’s branch was deleted. /// - [Parameter(Value = "head_ref_deleted")] HeadRefDeleted, /// /// The pull request’s branch was restored. /// - [Parameter(Value = "head_ref_restored")] HeadRefRestored, } } \ No newline at end of file From 46e572131f7175b3a85a6a86d62b139c49d22111 Mon Sep 17 00:00:00 2001 From: Jan Ove Skogheim Date: Wed, 25 Feb 2015 22:36:18 +0100 Subject: [PATCH 2/2] Ignore underscores when parsing enums under deserialization. --- Octokit.Tests/SimpleJsonSerializerTests.cs | 8 ++++++++ Octokit/Http/SimpleJsonSerializer.cs | 13 ++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) 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()