mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-08 04:40:54 +00:00
Merge pull request #1402 from maddin2016/fix-serialize-value-attribute
Fix serialization of enum value attributes
This commit is contained in:
@@ -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>(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>(reaction);
|
||||
Assert.Equal(reactionType, reaction.Content);
|
||||
Assert.Equal(result.User.Id, reaction.User.Id);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -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<Reaction>(issueCommentReaction);
|
||||
|
||||
Assert.Equal(ReactionType.Heart, issueCommentReaction.Content);
|
||||
|
||||
Assert.Equal(issueComment.User.Id, issueCommentReaction.User.Id);
|
||||
Assert.IsType<Reaction>(reaction);
|
||||
Assert.Equal(reactionType, reaction.Content);
|
||||
Assert.Equal(issueComment.User.Id, reaction.User.Id);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -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<Reaction>(issueReaction);
|
||||
|
||||
Assert.Equal(ReactionType.Heart, issueReaction.Content);
|
||||
|
||||
Assert.Equal(issue.User.Id, issueReaction.User.Id);
|
||||
Assert.IsType<Reaction>(reaction);
|
||||
Assert.Equal(reactionType, reaction.Content);
|
||||
Assert.Equal(issue.User.Id, reaction.User.Id);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -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<Reaction>(pullRequestReviewCommentReaction);
|
||||
|
||||
Assert.Equal(ReactionType.Heart, pullRequestReviewCommentReaction.Content);
|
||||
|
||||
Assert.Equal(commentFromGitHub.User.Id, pullRequestReviewCommentReaction.User.Id);
|
||||
Assert.IsType<Reaction>(reaction);
|
||||
Assert.Equal(reactionType, reaction.Content);
|
||||
Assert.Equal(commentFromGitHub.User.Id, reaction.User.Id);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -24,6 +24,7 @@ namespace Octokit.Internal
|
||||
class GitHubSerializerStrategy : PocoJsonSerializerStrategy
|
||||
{
|
||||
readonly List<string> _membersWhichShouldPublishNull = new List<string>();
|
||||
Dictionary<Type, Dictionary<object, object>> _cachedEnums = new Dictionary<Type, Dictionary<object, object>>();
|
||||
|
||||
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<object, object>());
|
||||
//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))
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace Octokit
|
||||
/// <summary>
|
||||
/// The reaction type (required)
|
||||
/// </summary>
|
||||
public ReactionType Content { get; private set; }
|
||||
public ReactionType Content { get; private set; }
|
||||
|
||||
internal string DebuggerDisplay
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user