Skip to content

Commit 419936f

Browse files
committed
fix: unknown FirebaseProviderType
Fixes #188
1 parent f972ba9 commit 419936f

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

src/Auth/FirebaseProviderType.cs

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Firebase.Auth
77
/// </summary>
88
public enum FirebaseProviderType
99
{
10+
Unknown,
11+
1012
[EnumMember(Value = "facebook.com")]
1113
Facebook,
1214

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

33+
[EnumMember(Value = "phone")]
34+
Phone,
35+
3136
Anonymous
3237
}
3338
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Newtonsoft.Json.Converters;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Reflection;
5+
6+
namespace Firebase.Auth.Requests.Converters
7+
{
8+
/// <inheritdoc />
9+
/// <summary>
10+
/// Defaults enum values to the base value if
11+
/// </summary>
12+
public class DefaultEnumConverter : StringEnumConverter
13+
{
14+
/// <summary>
15+
/// The default value used to fallback on when a enum is not convertable.
16+
/// </summary>
17+
private readonly int defaultValue;
18+
19+
/// <inheritdoc />
20+
/// <summary>
21+
/// Default constructor. Defaults the default value to 0.
22+
/// </summary>
23+
public DefaultEnumConverter()
24+
{ }
25+
26+
/// <inheritdoc />
27+
/// <summary>
28+
/// Sets the default value for the enum value.
29+
/// </summary>
30+
/// <param name="defaultValue">The default value to use.</param>
31+
public DefaultEnumConverter(int defaultValue)
32+
{
33+
this.defaultValue = defaultValue;
34+
}
35+
36+
/// <inheritdoc />
37+
/// <summary>
38+
/// Reads the provided JSON and attempts to convert using StringEnumConverter. If that fails set the value to the default value.
39+
/// </summary>
40+
/// <param name="reader">Reads the JSON value.</param>
41+
/// <param name="objectType">Current type that is being converted.</param>
42+
/// <param name="existingValue">The existing value being read.</param>
43+
/// <param name="serializer">Instance of the JSON Serializer.</param>
44+
/// <returns>The deserialized value of the enum if it exists or the default value if it does not.</returns>
45+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
46+
{
47+
try
48+
{
49+
return base.ReadJson(reader, objectType, existingValue, serializer);
50+
}
51+
catch
52+
{
53+
return Enum.Parse(objectType, $"{defaultValue}");
54+
}
55+
}
56+
57+
/// <inheritdoc />
58+
/// <summary>
59+
/// Validates that this converter can handle the type that is being provided.
60+
/// </summary>
61+
/// <param name="objectType">The type of the object being converted.</param>
62+
/// <returns>True if the base class says so, and if the value is an enum and has a default value to fall on.</returns>
63+
public override bool CanConvert(Type objectType)
64+
{
65+
return base.CanConvert(objectType) && objectType.GetTypeInfo().IsEnum && Enum.IsDefined(objectType, defaultValue);
66+
}
67+
}
68+
}

src/Auth/Requests/JavaScriptDateTimeConverter.cs src/Auth/Requests/Converters/JavaScriptDateTimeConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Newtonsoft.Json;
22
using System;
33

4-
namespace Firebase.Auth.Requests
4+
namespace Firebase.Auth.Requests.Converters
55
{
66
internal class JavaScriptDateTimeConverter : JsonConverter
77
{

src/Auth/Requests/GetAccountInfo.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Newtonsoft.Json;
1+
using Firebase.Auth.Requests.Converters;
2+
using Newtonsoft.Json;
23
using System;
34

45
namespace Firebase.Auth.Requests
@@ -36,6 +37,7 @@ public class GetAccountInfoResponseUserInfo
3637

3738
public class ProviderUserInfo
3839
{
40+
[JsonConverter(typeof(DefaultEnumConverter))]
3941
public FirebaseProviderType ProviderId { get; set; }
4042

4143
public string DisplayName { get; set; }

0 commit comments

Comments
 (0)