Repository: sendgrid/sendgrid-java (java)

Issue:
class TrackingSettings use wrong jackson annotation
com.sendgrid.TrackingSettings use @JsonInclude(Include.NON_DEFAULT)
so when you set
OpenTrackingSetting setting1 = new OpenTrackingSetting();
setting1.setEnable(false);
settings.setOpenTrackingSetting(setting1);
ClickTrackingSetting setting2 = new ClickTrackingSetting();
setting2.setEnable(false);

the jackson will give a empty jsonobject, and the http request will get response 400

Pull request:
Changes serialization type from default to non-empty.
Fixes #216.

Settings will be serialized anyway even if no parameters are set and defaults are used.

Patch:
diff --git a/src/main/java/com/sendgrid/helpers/mail/objects/BccSettings.java b/src/main/java/com/sendgrid/helpers/mail/objects/BccSettings.java
index 37be086..7d3d1d6 100644
--- a/src/main/java/com/sendgrid/helpers/mail/objects/BccSettings.java
+++ b/src/main/java/com/sendgrid/helpers/mail/objects/BccSettings.java
@@ -9,7 +9,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
  * automatically sent to the specified email address 
  * for every email that is sent.
  */
-@JsonInclude(Include.NON_DEFAULT)
+@JsonInclude(Include.NON_EMPTY)
 public class BccSettings {
   @JsonProperty("enable") private boolean enable;
   @JsonProperty("email") private String email;
diff --git a/src/main/java/com/sendgrid/helpers/mail/objects/ClickTrackingSetting.java b/src/main/java/com/sendgrid/helpers/mail/objects/ClickTrackingSetting.java
index 2c139cd..b8bd4d3 100644
--- a/src/main/java/com/sendgrid/helpers/mail/objects/ClickTrackingSetting.java
+++ b/src/main/java/com/sendgrid/helpers/mail/objects/ClickTrackingSetting.java
@@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
  * Settings to determine how you would like to track the 
  * metrics of how your recipients interact with your email.
  */
+@JsonInclude(Include.NON_EMPTY)
 public class ClickTrackingSetting {
   @JsonProperty("enable") private boolean enable;
   @JsonProperty("enable_text") private boolean enableText;
diff --git a/src/main/java/com/sendgrid/helpers/mail/objects/FooterSetting.java b/src/main/java/com/sendgrid/helpers/mail/objects/FooterSetting.java
index 1428b66..dc965d6 100644
--- a/src/main/java/com/sendgrid/helpers/mail/objects/FooterSetting.java
+++ b/src/main/java/com/sendgrid/helpers/mail/objects/FooterSetting.java
@@ -8,7 +8,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
  * An object representing the default footer 
  * that you would like included on every email.
  */
-@JsonInclude(Include.NON_DEFAULT)
+@JsonInclude(Include.NON_EMPTY)
 public class FooterSetting {
   @JsonProperty("enable") private boolean enable;
   @JsonProperty("text") private String text;
diff --git a/src/main/java/com/sendgrid/helpers/mail/objects/GoogleAnalyticsSetting.java b/src/main/java/com/sendgrid/helpers/mail/objects/GoogleAnalyticsSetting.java
index eac9189..3f98636 100644
--- a/src/main/java/com/sendgrid/helpers/mail/objects/GoogleAnalyticsSetting.java
+++ b/src/main/java/com/sendgrid/helpers/mail/objects/GoogleAnalyticsSetting.java
@@ -7,7 +7,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
 /**
  * An object configuring the tracking provided by Google Analytics.
  */
-@JsonInclude(Include.NON_DEFAULT)
+@JsonInclude(Include.NON_EMPTY)
 public class GoogleAnalyticsSetting {
   @JsonProperty("enable") private boolean enable;
   @JsonProperty("utm_source") private String campaignSource;
diff --git a/src/main/java/com/sendgrid/helpers/mail/objects/OpenTrackingSetting.java b/src/main/java/com/sendgrid/helpers/mail/objects/OpenTrackingSetting.java
index deba6d3..bbddd48 100644
--- a/src/main/java/com/sendgrid/helpers/mail/objects/OpenTrackingSetting.java
+++ b/src/main/java/com/sendgrid/helpers/mail/objects/OpenTrackingSetting.java
@@ -10,7 +10,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
  * pixel image in the body of the content. When the pixel is 
  * loaded, we can log that the email was opened.
  */
-@JsonInclude(Include.NON_DEFAULT)
+@JsonInclude(Include.NON_EMPTY)
 public class OpenTrackingSetting {
   @JsonProperty("enable") private boolean enable;
   @JsonProperty("substitution_tag") private String substitutionTag;
diff --git a/src/main/java/com/sendgrid/helpers/mail/objects/SpamCheckSetting.java b/src/main/java/com/sendgrid/helpers/mail/objects/SpamCheckSetting.java
index dbd4395..f0c6ec9 100644
--- a/src/main/java/com/sendgrid/helpers/mail/objects/SpamCheckSetting.java
+++ b/src/main/java/com/sendgrid/helpers/mail/objects/SpamCheckSetting.java
@@ -8,7 +8,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
  * A setting object that allows you to test the content of 
  * your email for spam.
  */
-@JsonInclude(Include.NON_DEFAULT)
+@JsonInclude(Include.NON_EMPTY)
 public class SpamCheckSetting {
   @JsonProperty("enable") private boolean enable;
   @JsonProperty("threshold") private int spamThreshold;
diff --git a/src/main/java/com/sendgrid/helpers/mail/objects/SubscriptionTrackingSetting.java b/src/main/java/com/sendgrid/helpers/mail/objects/SubscriptionTrackingSetting.java
index 304eb91..3f74026 100644
--- a/src/main/java/com/sendgrid/helpers/mail/objects/SubscriptionTrackingSetting.java
+++ b/src/main/java/com/sendgrid/helpers/mail/objects/SubscriptionTrackingSetting.java
@@ -11,7 +11,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
  * would like to specify the location of the link within your 
  * email, you may use the substitution_tag.
  */
-@JsonInclude(Include.NON_DEFAULT)
+@JsonInclude(Include.NON_EMPTY)
 public class SubscriptionTrackingSetting {
   @JsonProperty("enable") private boolean enable;
   @JsonProperty("text") private String text;


Test patch:
diff --git a/src/test/java/com/sendgrid/helpers/mail/objects/SettingsSerializationTest.java b/src/test/java/com/sendgrid/helpers/mail/objects/SettingsSerializationTest.java
new file mode 100644
index 0000000..74e6c8b
--- /dev/null
+++ b/src/test/java/com/sendgrid/helpers/mail/objects/SettingsSerializationTest.java
@@ -0,0 +1,86 @@
+package com.sendgrid.helpers.mail.objects;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.sendgrid.BccSettings;
+import com.sendgrid.ClickTrackingSetting;
+import com.sendgrid.FooterSetting;
+import com.sendgrid.GoogleAnalyticsSetting;
+import com.sendgrid.OpenTrackingSetting;
+import com.sendgrid.SpamCheckSetting;
+import com.sendgrid.SubscriptionTrackingSetting;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SettingsSerializationTest {
+
+	private ObjectMapper mapper = new ObjectMapper();
+
+	@Test
+	public void testOpenTrackingSettingSerialization() throws Exception {
+		OpenTrackingSetting setting = new OpenTrackingSetting();
+		setting.setEnable(false);
+
+		String json = mapper.writeValueAsString(setting);
+		Assert.assertEquals(json, "{\"enable\":false}");
+	}
+
+	@Test
+	public void testClickTrackingSettingSerialization() throws Exception {
+		ClickTrackingSetting setting = new ClickTrackingSetting();
+		setting.setEnable(false);
+
+		String json = mapper.writeValueAsString(setting);
+		System.out.println(json);
+		Assert.assertEquals(json, "{\"enable\":false,\"enable_text\":false}");
+	}
+
+	@Test
+	public void testSubscriptionTrackingSettingSerialization() throws Exception {
+		SubscriptionTrackingSetting setting = new SubscriptionTrackingSetting();
+		setting.setEnable(false);
+
+		String json = mapper.writeValueAsString(setting);
+		System.out.println(json);
+		Assert.assertEquals(json, "{\"enable\":false}");
+	}
+
+	@Test
+	public void testGoogleAnalyticsTrackingSettingSerialization() throws Exception {
+		GoogleAnalyticsSetting setting = new GoogleAnalyticsSetting();
+		setting.setEnable(false);
+
+		String json = mapper.writeValueAsString(setting);
+		System.out.println(json);
+		Assert.assertEquals(json, "{\"enable\":false}");
+	}
+
+	@Test
+	public void testSpamCheckSettingSerialization() throws Exception {
+		SpamCheckSetting setting = new SpamCheckSetting();
+		setting.setEnable(false);
+
+		String json = mapper.writeValueAsString(setting);
+		System.out.println(json);
+		Assert.assertEquals(json, "{\"enable\":false,\"threshold\":0}");
+	}
+
+	@Test
+	public void testFooterSettingSerialization() throws Exception {
+		FooterSetting setting = new FooterSetting();
+		setting.setEnable(false);
+
+		String json = mapper.writeValueAsString(setting);
+		System.out.println(json);
+		Assert.assertEquals(json, "{\"enable\":false}");
+	}
+
+	@Test
+	public void testBccSettingsSerialization() throws Exception {
+		BccSettings settings = new BccSettings();
+		settings.setEnable(false);
+
+		String json = mapper.writeValueAsString(settings);
+		System.out.println(json);
+		Assert.assertEquals(json, "{\"enable\":false}");
+	}
+}