From 4c05be81d43c38ae447db0e90d85fc44233a9d3d Mon Sep 17 00:00:00 2001 From: Martin Scholz Date: Wed, 22 Jun 2016 11:44:20 +0200 Subject: [PATCH] fix deserialization of enums with custom attributes --- Octokit/Http/SimpleJsonSerializer.cs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Octokit/Http/SimpleJsonSerializer.cs b/Octokit/Http/SimpleJsonSerializer.cs index 825279b5..d81dc258 100644 --- a/Octokit/Http/SimpleJsonSerializer.cs +++ b/Octokit/Http/SimpleJsonSerializer.cs @@ -80,17 +80,15 @@ namespace Octokit.Internal { var type = p.GetType(); var name = Enum.GetName(type, p); -#if NETFX_CORE - var attr = type.GetTypeInfo().GetCustomAttribute(); -#else - var attr = type.GetField(name) + + var attr = type.GetRuntimeField(name) .GetCustomAttributes(false) .OfType() .SingleOrDefault(); -#endif + if (attr != null) return attr.Value; - + return p.ToString().ToLowerInvariant(); } @@ -105,6 +103,17 @@ namespace Octokit.Internal { if (ReflectionUtils.GetTypeInfo(type).IsEnum) { + //first try to get all custom attributes + var fields = type.GetRuntimeFields(); + foreach (var field in fields) + { + var attribute = (ParameterAttribute)field.GetCustomAttribute(typeof(ParameterAttribute)); + if (attribute != null) + { + if (attribute.Value.Equals(value)) + return field.GetValue(null); + } + } // remove '-' from values coming in to be able to enum utf-8 stringValue = RemoveHyphenAndUnderscore(stringValue); return Enum.Parse(type, stringValue, ignoreCase: true);