Skip to content

Commit

Permalink
UserProfile.Fields left empty when deserializing an empty array
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
soxtoby committed Jul 23, 2019
1 parent 28544a6 commit 62e8557
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
25 changes: 25 additions & 0 deletions SlackNet.Tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ public void IgnoreIfDefault_NotDefault_Serialized()
result.ShouldBe(@"{""value"":""test_value""}");
}

[Test]
public void UserProfileFields_Null_IsNull()
{
var result = JsonConvert.DeserializeObject<UserProfile>(@"{""fields"":null,""phone"":""123""}");
result.Fields.ShouldBeNull();
result.Phone.ShouldBe("123");
}

[Test]
public void UserProfileFields_EmptyArray_IsEmpty()
{
var result = JsonConvert.DeserializeObject<UserProfile>(@"{""fields"":[],""phone"":""123""}");
result.Fields.ShouldBeEmpty();
result.Phone.ShouldBe("123");
}

[Test]
public void UserProfileFields_PopulatedObject_IsPopulated()
{
var result = JsonConvert.DeserializeObject<UserProfile>(@"{""fields"":{""fieldId"":{""value"":""foo""}},""phone"":""123""}");
result.Fields.Keys.ShouldMatch(new[] { "fieldId" });
result.Fields["fieldId"].Value.ShouldBe("foo");
result.Phone.ShouldBe("123");
}

class SimpleType
{
public string SomeProperty { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions SlackNet/Objects/UserProfile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace SlackNet
{
Expand All @@ -21,6 +22,7 @@ public class UserProfile
public string Image72 { get; set; }
public string Image192 { get; set; }
public string Image512 { get; set; }
[JsonConverter(typeof(IgnoreArrayConverter))] // Slack returns an empty array instead of an object if profile has been modified, but no custom fields have been set
public IDictionary<string, UserProfileField> Fields { get; set; } = new Dictionary<string, UserProfileField>();
}
}
31 changes: 31 additions & 0 deletions SlackNet/Serialization/IgnoreArrayConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json;

namespace SlackNet
{
class IgnoreArrayConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
reader.Skip();
return existingValue;
}

return serializer.Deserialize(reader, objectType);
}

public override bool CanConvert(Type objectType)
{
return typeof(IDictionary<,>).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
}
}
}

0 comments on commit 62e8557

Please sign in to comment.