Skip to content

Commit

Permalink
fix: unknown FirebaseProviderType
Browse files Browse the repository at this point in the history
Fixes #188
  • Loading branch information
bezysoftware committed Feb 5, 2023
1 parent f972ba9 commit 419936f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Auth/FirebaseProviderType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Firebase.Auth
/// </summary>
public enum FirebaseProviderType
{
Unknown,

[EnumMember(Value = "facebook.com")]
Facebook,

Expand All @@ -28,6 +30,9 @@ public enum FirebaseProviderType
[EnumMember(Value = "password")]
EmailAndPassword,

[EnumMember(Value = "phone")]
Phone,

Anonymous
}
}
68 changes: 68 additions & 0 deletions src/Auth/Requests/Converters/DefaultEnumConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System;
using System.Reflection;

namespace Firebase.Auth.Requests.Converters
{
/// <inheritdoc />
/// <summary>
/// Defaults enum values to the base value if
/// </summary>
public class DefaultEnumConverter : StringEnumConverter
{
/// <summary>
/// The default value used to fallback on when a enum is not convertable.
/// </summary>
private readonly int defaultValue;

/// <inheritdoc />
/// <summary>
/// Default constructor. Defaults the default value to 0.
/// </summary>
public DefaultEnumConverter()
{ }

/// <inheritdoc />
/// <summary>
/// Sets the default value for the enum value.
/// </summary>
/// <param name="defaultValue">The default value to use.</param>
public DefaultEnumConverter(int defaultValue)
{
this.defaultValue = defaultValue;
}

/// <inheritdoc />
/// <summary>
/// Reads the provided JSON and attempts to convert using StringEnumConverter. If that fails set the value to the default value.
/// </summary>
/// <param name="reader">Reads the JSON value.</param>
/// <param name="objectType">Current type that is being converted.</param>
/// <param name="existingValue">The existing value being read.</param>
/// <param name="serializer">Instance of the JSON Serializer.</param>
/// <returns>The deserialized value of the enum if it exists or the default value if it does not.</returns>
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}");
}
}

/// <inheritdoc />
/// <summary>
/// Validates that this converter can handle the type that is being provided.
/// </summary>
/// <param name="objectType">The type of the object being converted.</param>
/// <returns>True if the base class says so, and if the value is an enum and has a default value to fall on.</returns>
public override bool CanConvert(Type objectType)
{
return base.CanConvert(objectType) && objectType.GetTypeInfo().IsEnum && Enum.IsDefined(objectType, defaultValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Newtonsoft.Json;
using System;

namespace Firebase.Auth.Requests
namespace Firebase.Auth.Requests.Converters
{
internal class JavaScriptDateTimeConverter : JsonConverter
{
Expand Down
4 changes: 3 additions & 1 deletion src/Auth/Requests/GetAccountInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Firebase.Auth.Requests.Converters;
using Newtonsoft.Json;
using System;

namespace Firebase.Auth.Requests
Expand Down Expand Up @@ -36,6 +37,7 @@ public class GetAccountInfoResponseUserInfo

public class ProviderUserInfo
{
[JsonConverter(typeof(DefaultEnumConverter))]
public FirebaseProviderType ProviderId { get; set; }

public string DisplayName { get; set; }
Expand Down

0 comments on commit 419936f

Please sign in to comment.