Skip to content

Commit

Permalink
Merge pull request #727 from janovesk/master
Browse files Browse the repository at this point in the history
Support the correct spelling of event types, take two.
  • Loading branch information
haacked committed Feb 26, 2015
2 parents a61727f + 46e5721 commit 20e372d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Octokit.Tests/SimpleJsonSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,16 @@ public void HandlesBase64EncodedStrings()
}
}


public class TheDeserializeMethod
{
[Fact]
public void DeserializesEventInfosWithUnderscoresInName()
{
const string json = "{\"event\":\"head_ref_deleted\"}";
new SimpleJsonSerializer().Deserialize<EventInfo>(json);
}

[Fact]
public void UnderstandsRubyCasing()
{
Expand Down
13 changes: 12 additions & 1 deletion Octokit/Http/SimpleJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
}
Expand All @@ -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<string, KeyValuePair<Type, ReflectionUtils.SetDelegate>> SetterValueFactory(Type type)
{
return type.GetPropertiesAndFields()
Expand Down
2 changes: 0 additions & 2 deletions Octokit/Models/Response/EventInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,11 @@ public enum EventInfoState
/// <summary>
/// The pull request’s branch was deleted.
/// </summary>
[Parameter(Value = "head_ref_deleted")]
HeadRefDeleted,

/// <summary>
/// The pull request’s branch was restored.
/// </summary>
[Parameter(Value = "head_ref_restored")]
HeadRefRestored,
}
}

0 comments on commit 20e372d

Please sign in to comment.