Skip to content

Commit

Permalink
fix: add EmbedTypeConverter with unknown handling
Browse files Browse the repository at this point in the history
Prevents messages with an unknown EmbedType from failing to unmarshal
  • Loading branch information
foxbot committed Jun 8, 2019
1 parent 1cc5d73 commit d287ed1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Discord.Net.Rest/API/Common/Embed.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma warning disable CS1591
#pragma warning disable CS1591
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Discord.Net.Converters;

namespace Discord.API
{
Expand All @@ -15,7 +15,7 @@ internal class Embed
public string Url { get; set; }
[JsonProperty("color")]
public uint? Color { get; set; }
[JsonProperty("type"), JsonConverter(typeof(StringEnumConverter))]
[JsonProperty("type"), JsonConverter(typeof(EmbedTypeConverter))]
public EmbedType Type { get; set; }
[JsonProperty("timestamp")]
public DateTimeOffset? Timestamp { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ private static JsonConverter GetConverter(JsonProperty property, PropertyInfo pr
return PermissionTargetConverter.Instance;
if (type == typeof(UserStatus))
return UserStatusConverter.Instance;
if (type == typeof(EmbedType))
return EmbedTypeConverter.Instance;

//Special
if (type == typeof(API.Image))
Expand Down
73 changes: 73 additions & 0 deletions src/Discord.Net.Rest/Net/Converters/EmbedTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using Newtonsoft.Json;

namespace Discord.Net.Converters
{
internal class EmbedTypeConverter : JsonConverter
{
public static readonly EmbedTypeConverter Instance = new EmbedTypeConverter();

public override bool CanConvert(Type objectType) => true;
public override bool CanRead => true;
public override bool CanWrite => true;

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
switch ((string)reader.Value)
{
case "rich":
return EmbedType.Rich;
case "link":
return EmbedType.Link;
case "video":
return EmbedType.Video;
case "image":
return EmbedType.Image;
case "gifv":
return EmbedType.Gifv;
case "article":
return EmbedType.Article;
case "tweet":
return EmbedType.Tweet;
case "html":
return EmbedType.Html;
case "application_news": // TODO 2.2 EmbedType.News
default:
return EmbedType.Unknown;
}
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
switch ((EmbedType)value)
{
case EmbedType.Rich:
writer.WriteValue("rich");
break;
case EmbedType.Link:
writer.WriteValue("link");
break;
case EmbedType.Video:
writer.WriteValue("video");
break;
case EmbedType.Image:
writer.WriteValue("image");
break;
case EmbedType.Gifv:
writer.WriteValue("gifv");
break;
case EmbedType.Article:
writer.WriteValue("article");
break;
case EmbedType.Tweet:
writer.WriteValue("tweet");
break;
case EmbedType.Html:
writer.WriteValue("html");
break;
default:
throw new JsonSerializationException("Invalid embed type");
}
}
}
}

0 comments on commit d287ed1

Please sign in to comment.