From 419936f8428d1a4efc750b319a00bb643423d42f Mon Sep 17 00:00:00 2001 From: Tomas Bezouska Date: Sun, 5 Feb 2023 17:54:22 +0100 Subject: [PATCH] fix: unknown FirebaseProviderType Fixes #188 --- src/Auth/FirebaseProviderType.cs | 5 ++ .../Converters/DefaultEnumConverter.cs | 68 +++++++++++++++++++ .../JavaScriptDateTimeConverter.cs | 2 +- src/Auth/Requests/GetAccountInfo.cs | 4 +- 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/Auth/Requests/Converters/DefaultEnumConverter.cs rename src/Auth/Requests/{ => Converters}/JavaScriptDateTimeConverter.cs (94%) diff --git a/src/Auth/FirebaseProviderType.cs b/src/Auth/FirebaseProviderType.cs index 7ccb503..7f4e0ab 100644 --- a/src/Auth/FirebaseProviderType.cs +++ b/src/Auth/FirebaseProviderType.cs @@ -7,6 +7,8 @@ namespace Firebase.Auth /// public enum FirebaseProviderType { + Unknown, + [EnumMember(Value = "facebook.com")] Facebook, @@ -28,6 +30,9 @@ public enum FirebaseProviderType [EnumMember(Value = "password")] EmailAndPassword, + [EnumMember(Value = "phone")] + Phone, + Anonymous } } diff --git a/src/Auth/Requests/Converters/DefaultEnumConverter.cs b/src/Auth/Requests/Converters/DefaultEnumConverter.cs new file mode 100644 index 0000000..033f6ca --- /dev/null +++ b/src/Auth/Requests/Converters/DefaultEnumConverter.cs @@ -0,0 +1,68 @@ +using Newtonsoft.Json.Converters; +using Newtonsoft.Json; +using System; +using System.Reflection; + +namespace Firebase.Auth.Requests.Converters +{ + /// + /// + /// Defaults enum values to the base value if + /// + public class DefaultEnumConverter : StringEnumConverter + { + /// + /// The default value used to fallback on when a enum is not convertable. + /// + private readonly int defaultValue; + + /// + /// + /// Default constructor. Defaults the default value to 0. + /// + public DefaultEnumConverter() + { } + + /// + /// + /// Sets the default value for the enum value. + /// + /// The default value to use. + public DefaultEnumConverter(int defaultValue) + { + this.defaultValue = defaultValue; + } + + /// + /// + /// Reads the provided JSON and attempts to convert using StringEnumConverter. If that fails set the value to the default value. + /// + /// Reads the JSON value. + /// Current type that is being converted. + /// The existing value being read. + /// Instance of the JSON Serializer. + /// The deserialized value of the enum if it exists or the default value if it does not. + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + try + { + return base.ReadJson(reader, objectType, existingValue, serializer); + } + catch + { + return Enum.Parse(objectType, $"{defaultValue}"); + } + } + + /// + /// + /// Validates that this converter can handle the type that is being provided. + /// + /// The type of the object being converted. + /// True if the base class says so, and if the value is an enum and has a default value to fall on. + public override bool CanConvert(Type objectType) + { + return base.CanConvert(objectType) && objectType.GetTypeInfo().IsEnum && Enum.IsDefined(objectType, defaultValue); + } + } +} diff --git a/src/Auth/Requests/JavaScriptDateTimeConverter.cs b/src/Auth/Requests/Converters/JavaScriptDateTimeConverter.cs similarity index 94% rename from src/Auth/Requests/JavaScriptDateTimeConverter.cs rename to src/Auth/Requests/Converters/JavaScriptDateTimeConverter.cs index 88d4072..4782304 100644 --- a/src/Auth/Requests/JavaScriptDateTimeConverter.cs +++ b/src/Auth/Requests/Converters/JavaScriptDateTimeConverter.cs @@ -1,7 +1,7 @@ using Newtonsoft.Json; using System; -namespace Firebase.Auth.Requests +namespace Firebase.Auth.Requests.Converters { internal class JavaScriptDateTimeConverter : JsonConverter { diff --git a/src/Auth/Requests/GetAccountInfo.cs b/src/Auth/Requests/GetAccountInfo.cs index ece21cb..e341eaa 100644 --- a/src/Auth/Requests/GetAccountInfo.cs +++ b/src/Auth/Requests/GetAccountInfo.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using Firebase.Auth.Requests.Converters; +using Newtonsoft.Json; using System; namespace Firebase.Auth.Requests @@ -36,6 +37,7 @@ public class GetAccountInfoResponseUserInfo public class ProviderUserInfo { + [JsonConverter(typeof(DefaultEnumConverter))] public FirebaseProviderType ProviderId { get; set; } public string DisplayName { get; set; }