From 62e85577582474c89781d45831e696ee2dc59725 Mon Sep 17 00:00:00 2001 From: Simon Oxtoby Date: Tue, 23 Jul 2019 21:43:11 +1000 Subject: [PATCH] UserProfile.Fields left empty when deserializing an empty array Fixes #15 --- SlackNet.Tests/SerializationTests.cs | 25 +++++++++++++++ SlackNet/Objects/UserProfile.cs | 2 ++ .../Serialization/IgnoreArrayConverter.cs | 31 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 SlackNet/Serialization/IgnoreArrayConverter.cs diff --git a/SlackNet.Tests/SerializationTests.cs b/SlackNet.Tests/SerializationTests.cs index ecc1267..d999b1f 100644 --- a/SlackNet.Tests/SerializationTests.cs +++ b/SlackNet.Tests/SerializationTests.cs @@ -127,6 +127,31 @@ public void IgnoreIfDefault_NotDefault_Serialized() result.ShouldBe(@"{""value"":""test_value""}"); } + [Test] + public void UserProfileFields_Null_IsNull() + { + var result = JsonConvert.DeserializeObject(@"{""fields"":null,""phone"":""123""}"); + result.Fields.ShouldBeNull(); + result.Phone.ShouldBe("123"); + } + + [Test] + public void UserProfileFields_EmptyArray_IsEmpty() + { + var result = JsonConvert.DeserializeObject(@"{""fields"":[],""phone"":""123""}"); + result.Fields.ShouldBeEmpty(); + result.Phone.ShouldBe("123"); + } + + [Test] + public void UserProfileFields_PopulatedObject_IsPopulated() + { + var result = JsonConvert.DeserializeObject(@"{""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; } diff --git a/SlackNet/Objects/UserProfile.cs b/SlackNet/Objects/UserProfile.cs index bc10d27..046791e 100644 --- a/SlackNet/Objects/UserProfile.cs +++ b/SlackNet/Objects/UserProfile.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Newtonsoft.Json; namespace SlackNet { @@ -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 Fields { get; set; } = new Dictionary(); } } \ No newline at end of file diff --git a/SlackNet/Serialization/IgnoreArrayConverter.cs b/SlackNet/Serialization/IgnoreArrayConverter.cs new file mode 100644 index 0000000..3dece46 --- /dev/null +++ b/SlackNet/Serialization/IgnoreArrayConverter.cs @@ -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()); + } + } +} \ No newline at end of file