diff --git a/Octokit.Tests.Integration/Clients/CommitCommentReactionsClientTests.cs b/Octokit.Tests.Integration/Clients/CommitCommentReactionsClientTests.cs index 14e749b6..679070a6 100644 --- a/Octokit.Tests.Integration/Clients/CommitCommentReactionsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/CommitCommentReactionsClientTests.cs @@ -56,14 +56,16 @@ public class CommitCommentReactionsClientTests Assert.NotNull(result); - var newReaction = new NewReaction(ReactionType.Confused); - var reaction = await _github.Reaction.CommitComment.Create(_context.RepositoryOwner, _context.RepositoryName, result.Id, newReaction); + foreach (ReactionType reactionType in Enum.GetValues(typeof(ReactionType))) + { + var newReaction = new NewReaction(reactionType); - Assert.IsType(reaction); + var reaction = await _github.Reaction.CommitComment.Create(_context.RepositoryOwner, _context.RepositoryName, result.Id, newReaction); - Assert.Equal(ReactionType.Confused, reaction.Content); - - Assert.Equal(result.User.Id, reaction.User.Id); + Assert.IsType(reaction); + Assert.Equal(reactionType, reaction.Content); + Assert.Equal(result.User.Id, reaction.User.Id); + } } public void Dispose() diff --git a/Octokit.Tests.Integration/Clients/IssueCommentReactionsClientTests.cs b/Octokit.Tests.Integration/Clients/IssueCommentReactionsClientTests.cs index 210a551e..6502e867 100644 --- a/Octokit.Tests.Integration/Clients/IssueCommentReactionsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/IssueCommentReactionsClientTests.cs @@ -33,15 +33,16 @@ public class IssueCommentReactionsClientTests Assert.NotNull(issueComment); - var issueCommentReaction = await _github.Reaction.IssueComment.Create(_context.RepositoryOwner, _context.RepositoryName, issueComment.Id, new NewReaction(ReactionType.Heart)); + foreach (ReactionType reactionType in Enum.GetValues(typeof(ReactionType))) + { + var newReaction = new NewReaction(reactionType); - Assert.NotNull(issueCommentReaction); + var reaction = await _github.Reaction.CommitComment.Create(_context.RepositoryOwner, _context.RepositoryName, issueComment.Id, newReaction); - Assert.IsType(issueCommentReaction); - - Assert.Equal(ReactionType.Heart, issueCommentReaction.Content); - - Assert.Equal(issueComment.User.Id, issueCommentReaction.User.Id); + Assert.IsType(reaction); + Assert.Equal(reactionType, reaction.Content); + Assert.Equal(issueComment.User.Id, reaction.User.Id); + } } public void Dispose() diff --git a/Octokit.Tests.Integration/Clients/IssueReactionsClientTests.cs b/Octokit.Tests.Integration/Clients/IssueReactionsClientTests.cs index 32f263fa..e6b9b806 100644 --- a/Octokit.Tests.Integration/Clients/IssueReactionsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/IssueReactionsClientTests.cs @@ -29,15 +29,16 @@ public class IssueReactionsClientTests Assert.NotNull(issue); - var issueReaction = await _github.Reaction.Issue.Create(_context.RepositoryOwner, _context.RepositoryName, issue.Number, new NewReaction(ReactionType.Heart)); + foreach (ReactionType reactionType in Enum.GetValues(typeof(ReactionType))) + { + var newReaction = new NewReaction(reactionType); - Assert.NotNull(issueReaction); + var reaction = await _github.Reaction.CommitComment.Create(_context.RepositoryOwner, _context.RepositoryName, issue.Id, newReaction); - Assert.IsType(issueReaction); - - Assert.Equal(ReactionType.Heart, issueReaction.Content); - - Assert.Equal(issue.User.Id, issueReaction.User.Id); + Assert.IsType(reaction); + Assert.Equal(reactionType, reaction.Content); + Assert.Equal(issue.User.Id, reaction.User.Id); + } } public void Dispose() diff --git a/Octokit.Tests.Integration/Clients/PullRequestReviewCommentReactionsClientTests.cs b/Octokit.Tests.Integration/Clients/PullRequestReviewCommentReactionsClientTests.cs index b705bfa8..84afbcda 100644 --- a/Octokit.Tests.Integration/Clients/PullRequestReviewCommentReactionsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/PullRequestReviewCommentReactionsClientTests.cs @@ -40,15 +40,16 @@ public class PullRequestReviewCommentReactionsClientTests : IDisposable AssertComment(commentFromGitHub, body, position); - var pullRequestReviewCommentReaction = await _github.Reaction.PullRequestReviewComment.Create(_context.RepositoryOwner, _context.RepositoryName, commentFromGitHub.Id, new NewReaction(ReactionType.Heart)); + foreach (ReactionType reactionType in Enum.GetValues(typeof(ReactionType))) + { + var newReaction = new NewReaction(reactionType); - Assert.NotNull(pullRequestReviewCommentReaction); + var reaction = await _github.Reaction.CommitComment.Create(_context.RepositoryOwner, _context.RepositoryName, commentFromGitHub.Id, newReaction); - Assert.IsType(pullRequestReviewCommentReaction); - - Assert.Equal(ReactionType.Heart, pullRequestReviewCommentReaction.Content); - - Assert.Equal(commentFromGitHub.User.Id, pullRequestReviewCommentReaction.User.Id); + Assert.IsType(reaction); + Assert.Equal(reactionType, reaction.Content); + Assert.Equal(commentFromGitHub.User.Id, reaction.User.Id); + } } /// diff --git a/Octokit/Http/SimpleJsonSerializer.cs b/Octokit/Http/SimpleJsonSerializer.cs index 5835bcac..8253bdad 100644 --- a/Octokit/Http/SimpleJsonSerializer.cs +++ b/Octokit/Http/SimpleJsonSerializer.cs @@ -24,6 +24,7 @@ namespace Octokit.Internal class GitHubSerializerStrategy : PocoJsonSerializerStrategy { readonly List _membersWhichShouldPublishNull = new List(); + Dictionary> _cachedEnums = new Dictionary>(); protected override string MapClrMemberToJsonFieldName(MemberInfo member) { @@ -78,7 +79,7 @@ namespace Octokit.Internal Justification = "The API expects lowercase values")] protected override object SerializeEnum(Enum p) { - return p.ToString().ToLowerInvariant(); + return p.ToParameter(); } private string _type; @@ -88,13 +89,43 @@ namespace Octokit.Internal { var stringValue = value as string; var jsonValue = value as JsonObject; + if (stringValue != null) { if (ReflectionUtils.GetTypeInfo(type).IsEnum) { - // remove '-' from values coming in to be able to enum utf-8 - stringValue = RemoveHyphenAndUnderscore(stringValue); - return Enum.Parse(type, stringValue, ignoreCase: true); + if (!_cachedEnums.ContainsKey(type)) + { + //First add type to Dictionary + _cachedEnums.Add(type, new Dictionary()); + //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; + } } if (ReflectionUtils.IsNullableType(type)) diff --git a/Octokit/Models/Request/NewReaction.cs b/Octokit/Models/Request/NewReaction.cs index 45607c14..7434da0c 100644 --- a/Octokit/Models/Request/NewReaction.cs +++ b/Octokit/Models/Request/NewReaction.cs @@ -18,7 +18,7 @@ namespace Octokit /// /// The reaction type (required) /// - public ReactionType Content { get; private set; } + public ReactionType Content { get; private set; } internal string DebuggerDisplay {