mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-03 11:05:56 +00:00
Fix deserializing of Emoji types (#2577)
This commit is contained in:
@@ -65,15 +65,15 @@ namespace Octokit.Tests.Clients
|
||||
[Fact]
|
||||
public async Task RequestsTheEmojiEndpoint()
|
||||
{
|
||||
IReadOnlyList<Emoji> response = new List<Emoji>
|
||||
IDictionary<string, string> response = new Dictionary<string, string>
|
||||
{
|
||||
{ new Emoji("foo", "http://example.com/foo.gif") },
|
||||
{ new Emoji("bar", "http://example.com/bar.gif") }
|
||||
{ "foo", "http://example.com/foo.gif" },
|
||||
{ "bar", "http://example.com/bar.gif" }
|
||||
};
|
||||
|
||||
var apiConnection = Substitute.For<IApiConnection>();
|
||||
apiConnection.GetAll<Emoji>(Args.Uri)
|
||||
.Returns(Task.FromResult(response));
|
||||
apiConnection.Get<IDictionary<string, string>>(Args.Uri)
|
||||
.Returns(Task.FromResult(response));
|
||||
|
||||
var client = new MiscellaneousClient(apiConnection);
|
||||
|
||||
@@ -82,7 +82,7 @@ namespace Octokit.Tests.Clients
|
||||
Assert.Equal(2, emojis.Count);
|
||||
Assert.Equal("foo", emojis[0].Name);
|
||||
apiConnection.Received()
|
||||
.GetAll<Emoji>(Arg.Is<Uri>(u => u.ToString() == "emojis"));
|
||||
.Get<IDictionary<string, string>>(Arg.Is<Uri>(u => u.ToString() == "emojis"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Octokit
|
||||
@@ -39,12 +41,13 @@ namespace Octokit
|
||||
/// Gets all the emojis available to use on GitHub.
|
||||
/// </summary>
|
||||
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
|
||||
/// <returns>An <see cref="IReadOnlyDictionary{TKey,TValue}"/> of emoji and their URI.</returns>
|
||||
/// <returns>An <see cref="IReadOnlyList{Emoji}"/> of emoji and their URI.</returns>
|
||||
[ManualRoute("GET", "/emojis")]
|
||||
[Obsolete("This client is being deprecated and will be removed in the future. Use EmojisClient.GetAllEmojis instead.")]
|
||||
public Task<IReadOnlyList<Emoji>> GetAllEmojis()
|
||||
public async Task<IReadOnlyList<Emoji>> GetAllEmojis()
|
||||
{
|
||||
return _emojisClient.GetAllEmojis();
|
||||
var result = await ApiConnection.Get<IDictionary<string, string>>(ApiUrls.Emojis());
|
||||
|
||||
return result.Select(x => new Emoji(x.Key, x.Value)).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Octokit
|
||||
{
|
||||
public static class AssignableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines whether the <paramref name="genericType"/> is assignable from
|
||||
/// <paramref name="givenType"/> taking into account generic definitions
|
||||
/// </summary>
|
||||
public static bool IsAssignableToGenericType(this Type givenType, Type genericType)
|
||||
{
|
||||
if (givenType == null || genericType == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return givenType == genericType
|
||||
|| givenType.MapsToGenericTypeDefinition(genericType)
|
||||
|| givenType.HasInterfaceThatMapsToGenericTypeDefinition(genericType)
|
||||
|| givenType.BaseType.IsAssignableToGenericType(genericType);
|
||||
}
|
||||
|
||||
private static bool HasInterfaceThatMapsToGenericTypeDefinition(this Type givenType, Type genericType)
|
||||
{
|
||||
return givenType
|
||||
.GetInterfaces()
|
||||
.Where(it => it.IsGenericType)
|
||||
.Any(it => it.GetGenericTypeDefinition() == genericType);
|
||||
}
|
||||
|
||||
private static bool MapsToGenericTypeDefinition(this Type givenType, Type genericType)
|
||||
{
|
||||
return genericType.IsGenericTypeDefinition
|
||||
&& givenType.IsGenericType
|
||||
&& givenType.GetGenericTypeDefinition() == genericType;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ namespace Octokit.Internal
|
||||
// simple json does not support the root node being empty. Will submit a pr but in the mean time....
|
||||
if (!string.IsNullOrEmpty(body) && body != "{}")
|
||||
{
|
||||
var typeIsDictionary = typeof(IDictionary).IsAssignableFrom(typeof(T));
|
||||
var typeIsDictionary = typeof(IDictionary).IsAssignableFrom(typeof(T)) || typeof(T).IsAssignableToGenericType(typeof(System.Collections.Generic.IDictionary<,>));
|
||||
var typeIsEnumerable = typeof(IEnumerable).IsAssignableFrom(typeof(T));
|
||||
var responseIsObject = body.StartsWith("{", StringComparison.Ordinal);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user