From 10bb81902251f3b028c071edd29bde1eb5a4a4da Mon Sep 17 00:00:00 2001 From: Logan Patino Date: Mon, 25 Mar 2019 11:36:34 -0400 Subject: [PATCH] fix: Only include nulls in serialization when processing API responses --- .../java/com/ibm/cloud/sdk/core/http/RequestBuilder.java | 8 ++++---- .../java/com/ibm/cloud/sdk/core/util/GsonSingleton.java | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/ibm/cloud/sdk/core/http/RequestBuilder.java b/src/main/java/com/ibm/cloud/sdk/core/http/RequestBuilder.java index 61070ae07..ebd0e1164 100644 --- a/src/main/java/com/ibm/cloud/sdk/core/http/RequestBuilder.java +++ b/src/main/java/com/ibm/cloud/sdk/core/http/RequestBuilder.java @@ -12,6 +12,7 @@ */ package com.ibm.cloud.sdk.core.http; +import com.google.gson.Gson; import com.google.gson.JsonObject; import com.ibm.cloud.sdk.core.service.BaseService; import com.ibm.cloud.sdk.core.util.GsonSingleton; @@ -333,12 +334,11 @@ public RequestBuilder bodyContent(InputStream stream, String contentType) { public RequestBuilder bodyContent(String contentType, Object jsonContent, Object jsonPatchContent, InputStream nonJsonContent) { if (contentType != null) { + Gson requestGson = GsonSingleton.getGson().newBuilder().serializeNulls().create(); if (BaseService.isJsonMimeType(contentType)) { - this.bodyContent( - GsonSingleton.getGson().toJsonTree(jsonContent).getAsJsonObject().toString(), contentType); + this.bodyContent(requestGson.toJsonTree(jsonContent).getAsJsonObject().toString(), contentType); } else if (BaseService.isJsonPatchMimeType(contentType)) { - this.bodyContent( - GsonSingleton.getGson().toJsonTree(jsonPatchContent).getAsJsonObject().toString(), contentType); + this.bodyContent(requestGson.toJsonTree(jsonPatchContent).getAsJsonObject().toString(), contentType); } else { this.bodyContent(nonJsonContent, contentType); } diff --git a/src/main/java/com/ibm/cloud/sdk/core/util/GsonSingleton.java b/src/main/java/com/ibm/cloud/sdk/core/util/GsonSingleton.java index a10281c4e..31d6c7fdd 100644 --- a/src/main/java/com/ibm/cloud/sdk/core/util/GsonSingleton.java +++ b/src/main/java/com/ibm/cloud/sdk/core/util/GsonSingleton.java @@ -45,7 +45,6 @@ private static Gson createGson(Boolean prettyPrint) { builder.setPrettyPrinting(); } builder.disableHtmlEscaping(); - builder.serializeNulls(); return builder.create(); }