-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not crash when User.Locale and UserInfo.Locale are an object (#699)
- Loading branch information
1 parent
020e3fc
commit 5d6efd2
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Auth0.Core/Serialization/StringOrObjectAsStringConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Auth0.Core.Serialization | ||
{ | ||
internal class StringOrObjectAsStringConverter : JsonConverter | ||
{ | ||
public override bool CanWrite => false; | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return true; | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var instance = ""; | ||
|
||
if (reader.TokenType == JsonToken.String) | ||
{ | ||
instance = reader.Value?.ToString(); | ||
} | ||
else if (reader.TokenType == JsonToken.StartObject) | ||
{ | ||
instance = JObject.Load(reader).ToString(); | ||
} | ||
|
||
return instance; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tests/Auth0.Core.UnitTests/StringOrObjectAsStringConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Auth0.Core.Serialization; | ||
using Newtonsoft.Json; | ||
using Xunit; | ||
|
||
namespace Auth0.Core.UnitTests; | ||
|
||
public class StringOrObjectAsStringConverterTests | ||
{ | ||
internal class StringOrObjectAsStringConverterData | ||
{ | ||
[JsonProperty("value")] | ||
[JsonConverter(typeof(StringOrObjectAsStringConverter))] | ||
public string Value { get; set; } | ||
} | ||
[Fact] | ||
public void Should_deserialize_string() | ||
{ | ||
var content = "{ 'value': 'test' }"; | ||
|
||
var parsed = JsonConvert.DeserializeObject<StringOrObjectAsStringConverterData>(content); | ||
|
||
Assert.Equal("test", parsed.Value); | ||
} | ||
|
||
[Fact] | ||
public void Should_deserialize_object_as_string() | ||
{ | ||
var content = "{ 'value': { 'innerValue': 'test' } }"; | ||
|
||
var parsed = JsonConvert.DeserializeObject<StringOrObjectAsStringConverterData>(content); | ||
|
||
Assert.Equal("{\n \"innerValue\": \"test\"\n}", parsed.Value); | ||
} | ||
|
||
[Fact] | ||
public void Should_deserialize_when_omitted() | ||
{ | ||
var content = "{}"; | ||
|
||
var parsed = JsonConvert.DeserializeObject<StringOrObjectAsStringConverterData>(content); | ||
|
||
Assert.Null(parsed.Value); | ||
} | ||
} |