diff --git a/src/Mandrill.net/Serialization/MandrillMergeVarConverter.cs b/src/Mandrill.net/Serialization/MandrillMergeVarConverter.cs index 10c34f8..647e46a 100644 --- a/src/Mandrill.net/Serialization/MandrillMergeVarConverter.cs +++ b/src/Mandrill.net/Serialization/MandrillMergeVarConverter.cs @@ -33,9 +33,18 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s writer.WriteStartObject(); writer.WritePropertyName("name"); writer.WriteValue(mergeVar.Name); - var content = JToken.FromObject((object)mergeVar.Content, _contentSerializer); writer.WritePropertyName("content"); - content.WriteTo(writer); + var content = (object)mergeVar.Content; + if (content == null) + { + writer.WriteNull(); + } + else + { + var token = JToken.FromObject(content, _contentSerializer); + token.WriteTo(writer); + } + writer.WriteEndObject(); } } diff --git a/tests/SerializationTests.cs b/tests/SerializationTests.cs index dad643f..4f20767 100644 --- a/tests/SerializationTests.cs +++ b/tests/SerializationTests.cs @@ -134,6 +134,27 @@ public void Can_serialize_merge_var_content_using_custom_settings() result["LastName"].Value().Should().BeNull(); } + [Fact] + public void Can_serialize_merge_var_with_null_content() + { + var message = new MandrillMessage(); + string data = null; + + message.GlobalMergeVars.Add(new MandrillMergeVar() + { + Name = "test", + Content = data + }); + + var json = JObject.FromObject(message, MandrillSerializer.Instance); + + json["global_merge_vars"].Should().NotBeEmpty(); + var result = json["global_merge_vars"].First["content"]; + + Output.WriteLine(json.ToString(Formatting.Indented)); + result.ToObject().Should().BeNull(); + } + [Fact] public void Can_set_property_name_by_convention() {