Skip to content

Commit 4ad3430

Browse files
authored
fix: Allow SendGridMessage serialization to optionally bypass defaults (#1064)
1 parent f8317ec commit 4ad3430

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/SendGrid/Helpers/Mail/SendGridMessage.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,8 +1024,9 @@ public void SetGoogleAnalytics(bool enable, string utmCampaign = null, string ut
10241024
/// <summary>
10251025
/// Creates the JSON object required to make a request to Twilio SendGrid.
10261026
/// </summary>
1027+
/// <param name="useDefaultSerialization">Whether or not to use JsonConvert.DefaultSettings for serialization.</param>
10271028
/// <returns>The JSON object required to make a request to Twilio SendGrid.</returns>
1028-
public string Serialize()
1029+
public string Serialize(bool useDefaultSerialization = true)
10291030
{
10301031
if (this.PlainTextContent != null || this.HtmlContent != null)
10311032
{
@@ -1085,6 +1086,14 @@ public string Serialize()
10851086
Formatting = Formatting.None
10861087
};
10871088

1089+
if (useDefaultSerialization)
1090+
{
1091+
return JsonConvert.SerializeObject(
1092+
this,
1093+
Formatting.None,
1094+
jsonSerializerSettings);
1095+
}
1096+
10881097
var jsonSerializer = JsonSerializer.Create(jsonSerializerSettings);
10891098

10901099
var stringBuilder = new StringBuilder(256);

tests/SendGrid.Tests/Integration.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ public void TestSendSingleEmailWithHelperWithOutEmailObject()
8484
// All paramaters available for sending an email
8585
[Fact]
8686
public void TestKitchenSink()
87+
{
88+
TestKitchenSinkInternal();
89+
}
90+
91+
// All paramaters available for sending an email
92+
private void TestKitchenSinkInternal(bool useDefaultSerialization = true)
8793
{
8894
var msg = new SendGridMessage();
8995
msg.SetFrom(new EmailAddress("[email protected]", "Example User1"));
@@ -328,7 +334,7 @@ public void TestKitchenSink()
328334
"some source",
329335
"some term");
330336
msg.SetReplyTo(new EmailAddress("[email protected]", "Reply To Me"));
331-
Assert.Equal("{\"from\":{\"name\":\"Example User1\",\"email\":\"[email protected]\"},\"subject\":\"Hello World from the Twilio SendGrid CSharp Library\",\"personalizations\":[{\"to\":[{\"name\":\"Example User2\",\"email\":\"[email protected]\"},{\"name\":\"Example User-2\",\"email\":\"[email protected]\"},{\"name\":\"Example User3\",\"email\":\"[email protected]\"},{\"name\":\"Example User4\",\"email\":\"[email protected]\"},{\"name\":\"Example User5\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User6\",\"email\":\"[email protected]\"},{\"name\":\"Example User7\",\"email\":\"[email protected]\"},{\"name\":\"Example User8\",\"email\":\"[email protected]\"},{\"name\":\"Example User9\",\"email\":\"[email protected]\"},{\"name\":\"Example User-9\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User10\",\"email\":\"test10example.com\"},{\"name\":\"Example User11\",\"email\":\"[email protected]\"},{\"name\":\"Example User12\",\"email\":\"[email protected]\"},{\"name\":\"Example User13\",\"email\":\"[email protected]\"},{\"name\":\"Example User-13\",\"email\":\"[email protected]\"}],\"subject\":\"Thank you for signing up, % name %\",\"headers\":{\"X-Test1\":\"True1\",\"X-Test2\":\"Test2\",\"X-Test3\":\"True3\",\"X-Test4\":\"True4\"},\"substitutions\":{\"%name1%\":\"Example User1\",\"%city2%\":\"Denver1\",\"%name3%\":\"Example User2\",\"%city4%\":\"Orange1\"},\"custom_args\":{\"marketing1\":\"false\",\"transactional1\":\"true\",\"marketing2\":\"true\",\"transactional2\":\"false\"},\"send_at\":1461775051},{\"to\":[{\"name\":\"Example User14\",\"email\":\"[email protected]\"},{\"name\":\"Example User15\",\"email\":\"[email protected]\"},{\"name\":\"Example User16\",\"email\":\"[email protected]\"},{\"name\":\"Example User17\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User18\",\"email\":\"[email protected]\"},{\"name\":\"Example User19\",\"email\":\"[email protected]\"},{\"name\":\"Example User20\",\"email\":\"[email protected]\"},{\"name\":\"Example User21\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User22\",\"email\":\"test22example.com\"},{\"name\":\"Example User23\",\"email\":\"[email protected]\"},{\"name\":\"Example User24\",\"email\":\"[email protected]\"},{\"name\":\"Example User25\",\"email\":\"[email protected]\"}],\"subject\":\"Thank you for signing up, % name % 2\",\"headers\":{\"X-Test5\":\"True5\",\"X-Test6\":\"Test6\",\"X-Test7\":\"True7\",\"X-Test8\":\"True8\"},\"substitutions\":{\"%name5%\":\"Example User5\",\"%city6%\":\"Denver6\",\"%name7%\":\"Example User7\",\"%city8%\":\"Orange8\"},\"custom_args\":{\"marketing3\":\"false\",\"transactional3\":\"true\",\"marketing4\":\"true\",\"transactional4\":\"false\"},\"send_at\":1461775052},{\"to\":[{\"name\":\"Example User26\",\"email\":\"[email protected]\"},{\"name\":\"Example User27\",\"email\":\"[email protected]\"},{\"name\":\"Example User28\",\"email\":\"[email protected]\"},{\"name\":\"Example User29\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User30\",\"email\":\"[email protected]\"},{\"name\":\"Example User31\",\"email\":\"[email protected]\"},{\"name\":\"Example User32\",\"email\":\"[email protected]\"},{\"name\":\"Example User33\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User34\",\"email\":\"test34example.com\"},{\"name\":\"Example User35\",\"email\":\"[email protected]\"},{\"name\":\"Example User36\",\"email\":\"[email protected]\"},{\"name\":\"Example User37\",\"email\":\"[email protected]\"}],\"subject\":\"Thank you for signing up, % name % 3\",\"headers\":{\"X-Test7\":\"True7\",\"X-Test8\":\"Test8\",\"X-Test9\":\"True9\",\"X-Test10\":\"True10\"},\"substitutions\":{\"%name9%\":\"Example User9\",\"%city10%\":\"Denver10\",\"%name11%\":\"Example User11\",\"%city12%\":\"Orange12\"},\"custom_args\":{\"marketing5\":\"false\",\"transactional5\":\"true\",\"marketing6\":\"true\",\"transactional6\":\"false\"},\"send_at\":1461775053}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Textual content\"},{\"type\":\"text/html\",\"value\":\"HTML content\"},{\"type\":\"text/calendar\",\"value\":\"Party Time!!\"},{\"type\":\"text/calendar2\",\"value\":\"Party Time2!!\"}],\"attachments\":[{\"content\":\"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12\",\"type\":\"application/pdf\",\"filename\":\"balance_001.pdf\",\"disposition\":\"attachment\",\"content_id\":\"Balance Sheet\"},{\"content\":\"BwdW\",\"type\":\"image/png\",\"filename\":\"banner.png\",\"disposition\":\"inline\",\"content_id\":\"Banner\"},{\"content\":\"BwdW2\",\"type\":\"image/png\",\"filename\":\"banner2.png\",\"disposition\":\"inline\",\"content_id\":\"Banner 2\"}],\"template_id\":\"13b8f94f-bcae-4ec6-b752-70d6cb59f932\",\"headers\":{\"X-Day\":\"Monday\",\"X-Month\":\"January\",\"X-Year\":\"2017\"},\"sections\":{\"%section1\":\"Substitution for Section 1 Tag\",\"%section2%\":\"Substitution for Section 2 Tag\",\"%section3%\":\"Substitution for Section 3 Tag\"},\"categories\":[\"customer\",\"vip\",\"new_account\"],\"custom_args\":{\"campaign\":\"welcome\",\"sequence2\":\"2\",\"sequence3\":\"3\"},\"send_at\":1461775051,\"asm\":{\"group_id\":3,\"groups_to_display\":[1,4,5]},\"batch_id\":\"some_batch_id\",\"ip_pool_name\":\"23\",\"mail_settings\":{\"bcc\":{\"enable\":true,\"email\":\"[email protected]\"},\"bypass_list_management\":{\"enable\":true},\"footer\":{\"enable\":true,\"text\":\"Some Footer Text\",\"html\":\"Some Footer HTML\"},\"sandbox_mode\":{\"enable\":true},\"spam_check\":{\"enable\":true,\"threshold\":1,\"post_to_url\":\"https://gotchya.example.com\"}},\"tracking_settings\":{\"click_tracking\":{\"enable\":true,\"enable_text\":false},\"open_tracking\":{\"enable\":true,\"substitution_tag\":\"Optional tag to replace with the open image in the body of the message\"},\"subscription_tracking\":{\"enable\":true,\"text\":\"text to insert into the text/plain portion of the message\",\"html\":\"HTML to insert into the text / html portion of the message\",\"substitution_tag\":\"substitution tag\"},\"ganalytics\":{\"enable\":true,\"utm_source\":\"some source\",\"utm_medium\":\"some medium\",\"utm_term\":\"some term\",\"utm_content\":\"some content\",\"utm_campaign\":\"some campaign\"}},\"reply_to\":{\"name\":\"Reply To Me\",\"email\":\"[email protected]\"}}", msg.Serialize());
337+
Assert.Equal("{\"from\":{\"name\":\"Example User1\",\"email\":\"[email protected]\"},\"subject\":\"Hello World from the Twilio SendGrid CSharp Library\",\"personalizations\":[{\"to\":[{\"name\":\"Example User2\",\"email\":\"[email protected]\"},{\"name\":\"Example User-2\",\"email\":\"[email protected]\"},{\"name\":\"Example User3\",\"email\":\"[email protected]\"},{\"name\":\"Example User4\",\"email\":\"[email protected]\"},{\"name\":\"Example User5\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User6\",\"email\":\"[email protected]\"},{\"name\":\"Example User7\",\"email\":\"[email protected]\"},{\"name\":\"Example User8\",\"email\":\"[email protected]\"},{\"name\":\"Example User9\",\"email\":\"[email protected]\"},{\"name\":\"Example User-9\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User10\",\"email\":\"test10example.com\"},{\"name\":\"Example User11\",\"email\":\"[email protected]\"},{\"name\":\"Example User12\",\"email\":\"[email protected]\"},{\"name\":\"Example User13\",\"email\":\"[email protected]\"},{\"name\":\"Example User-13\",\"email\":\"[email protected]\"}],\"subject\":\"Thank you for signing up, % name %\",\"headers\":{\"X-Test1\":\"True1\",\"X-Test2\":\"Test2\",\"X-Test3\":\"True3\",\"X-Test4\":\"True4\"},\"substitutions\":{\"%name1%\":\"Example User1\",\"%city2%\":\"Denver1\",\"%name3%\":\"Example User2\",\"%city4%\":\"Orange1\"},\"custom_args\":{\"marketing1\":\"false\",\"transactional1\":\"true\",\"marketing2\":\"true\",\"transactional2\":\"false\"},\"send_at\":1461775051},{\"to\":[{\"name\":\"Example User14\",\"email\":\"[email protected]\"},{\"name\":\"Example User15\",\"email\":\"[email protected]\"},{\"name\":\"Example User16\",\"email\":\"[email protected]\"},{\"name\":\"Example User17\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User18\",\"email\":\"[email protected]\"},{\"name\":\"Example User19\",\"email\":\"[email protected]\"},{\"name\":\"Example User20\",\"email\":\"[email protected]\"},{\"name\":\"Example User21\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User22\",\"email\":\"test22example.com\"},{\"name\":\"Example User23\",\"email\":\"[email protected]\"},{\"name\":\"Example User24\",\"email\":\"[email protected]\"},{\"name\":\"Example User25\",\"email\":\"[email protected]\"}],\"subject\":\"Thank you for signing up, % name % 2\",\"headers\":{\"X-Test5\":\"True5\",\"X-Test6\":\"Test6\",\"X-Test7\":\"True7\",\"X-Test8\":\"True8\"},\"substitutions\":{\"%name5%\":\"Example User5\",\"%city6%\":\"Denver6\",\"%name7%\":\"Example User7\",\"%city8%\":\"Orange8\"},\"custom_args\":{\"marketing3\":\"false\",\"transactional3\":\"true\",\"marketing4\":\"true\",\"transactional4\":\"false\"},\"send_at\":1461775052},{\"to\":[{\"name\":\"Example User26\",\"email\":\"[email protected]\"},{\"name\":\"Example User27\",\"email\":\"[email protected]\"},{\"name\":\"Example User28\",\"email\":\"[email protected]\"},{\"name\":\"Example User29\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User30\",\"email\":\"[email protected]\"},{\"name\":\"Example User31\",\"email\":\"[email protected]\"},{\"name\":\"Example User32\",\"email\":\"[email protected]\"},{\"name\":\"Example User33\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User34\",\"email\":\"test34example.com\"},{\"name\":\"Example User35\",\"email\":\"[email protected]\"},{\"name\":\"Example User36\",\"email\":\"[email protected]\"},{\"name\":\"Example User37\",\"email\":\"[email protected]\"}],\"subject\":\"Thank you for signing up, % name % 3\",\"headers\":{\"X-Test7\":\"True7\",\"X-Test8\":\"Test8\",\"X-Test9\":\"True9\",\"X-Test10\":\"True10\"},\"substitutions\":{\"%name9%\":\"Example User9\",\"%city10%\":\"Denver10\",\"%name11%\":\"Example User11\",\"%city12%\":\"Orange12\"},\"custom_args\":{\"marketing5\":\"false\",\"transactional5\":\"true\",\"marketing6\":\"true\",\"transactional6\":\"false\"},\"send_at\":1461775053}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Textual content\"},{\"type\":\"text/html\",\"value\":\"HTML content\"},{\"type\":\"text/calendar\",\"value\":\"Party Time!!\"},{\"type\":\"text/calendar2\",\"value\":\"Party Time2!!\"}],\"attachments\":[{\"content\":\"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12\",\"type\":\"application/pdf\",\"filename\":\"balance_001.pdf\",\"disposition\":\"attachment\",\"content_id\":\"Balance Sheet\"},{\"content\":\"BwdW\",\"type\":\"image/png\",\"filename\":\"banner.png\",\"disposition\":\"inline\",\"content_id\":\"Banner\"},{\"content\":\"BwdW2\",\"type\":\"image/png\",\"filename\":\"banner2.png\",\"disposition\":\"inline\",\"content_id\":\"Banner 2\"}],\"template_id\":\"13b8f94f-bcae-4ec6-b752-70d6cb59f932\",\"headers\":{\"X-Day\":\"Monday\",\"X-Month\":\"January\",\"X-Year\":\"2017\"},\"sections\":{\"%section1\":\"Substitution for Section 1 Tag\",\"%section2%\":\"Substitution for Section 2 Tag\",\"%section3%\":\"Substitution for Section 3 Tag\"},\"categories\":[\"customer\",\"vip\",\"new_account\"],\"custom_args\":{\"campaign\":\"welcome\",\"sequence2\":\"2\",\"sequence3\":\"3\"},\"send_at\":1461775051,\"asm\":{\"group_id\":3,\"groups_to_display\":[1,4,5]},\"batch_id\":\"some_batch_id\",\"ip_pool_name\":\"23\",\"mail_settings\":{\"bcc\":{\"enable\":true,\"email\":\"[email protected]\"},\"bypass_list_management\":{\"enable\":true},\"footer\":{\"enable\":true,\"text\":\"Some Footer Text\",\"html\":\"Some Footer HTML\"},\"sandbox_mode\":{\"enable\":true},\"spam_check\":{\"enable\":true,\"threshold\":1,\"post_to_url\":\"https://gotchya.example.com\"}},\"tracking_settings\":{\"click_tracking\":{\"enable\":true,\"enable_text\":false},\"open_tracking\":{\"enable\":true,\"substitution_tag\":\"Optional tag to replace with the open image in the body of the message\"},\"subscription_tracking\":{\"enable\":true,\"text\":\"text to insert into the text/plain portion of the message\",\"html\":\"HTML to insert into the text / html portion of the message\",\"substitution_tag\":\"substitution tag\"},\"ganalytics\":{\"enable\":true,\"utm_source\":\"some source\",\"utm_medium\":\"some medium\",\"utm_term\":\"some term\",\"utm_content\":\"some content\",\"utm_campaign\":\"some campaign\"}},\"reply_to\":{\"name\":\"Reply To Me\",\"email\":\"[email protected]\"}}", msg.Serialize(useDefaultSerialization));
332338
}
333339

334340
[Fact]
@@ -343,7 +349,7 @@ public void TestKitchenSinkIsUnaffectedByCustomContractResolver()
343349

344350
try
345351
{
346-
TestKitchenSink();
352+
TestKitchenSinkInternal(false);
347353

348354
// Ensure default behavior is not broken
349355
var testObject = new

0 commit comments

Comments
 (0)