Skip to content

Commit

Permalink
Merge pull request #118 from feinoujc/null-fix
Browse files Browse the repository at this point in the history
fix: null check before attempting jtoken
  • Loading branch information
feinoujc authored May 12, 2020
2 parents f3357dd + a7bfe60 commit e48bd15
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Mandrill.net/Serialization/MandrillMergeVarConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,27 @@ public void Can_serialize_merge_var_content_using_custom_settings()
result["LastName"].Value<string>().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<object>().Should().BeNull();
}

[Fact]
public void Can_set_property_name_by_convention()
{
Expand Down

0 comments on commit e48bd15

Please sign in to comment.