Skip to content

Commit a6d6541

Browse files
authored
fix: Updating json request deserialization to allow for list parsing (#75)
1 parent 5e8e603 commit a6d6541

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/main/java/com/ibm/cloud/sdk/core/http/RequestBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,11 @@ public RequestBuilder bodyContent(InputStream stream, String contentType) {
337337
public RequestBuilder bodyContent(String contentType, Object jsonContent, Object jsonPatchContent,
338338
InputStream nonJsonContent) {
339339
if (contentType != null) {
340-
Gson requestGson = GsonSingleton.getGson().newBuilder().create();
340+
Gson requestGson = GsonSingleton.getGsonWithoutPrettyPrinting().newBuilder().create();
341341
if (BaseService.isJsonMimeType(contentType)) {
342-
this.bodyContent(requestGson.toJsonTree(jsonContent).getAsJsonObject().toString(), contentType);
342+
this.bodyContent(requestGson.toJson(jsonContent), contentType);
343343
} else if (BaseService.isJsonPatchMimeType(contentType)) {
344-
this.bodyContent(requestGson.toJsonTree(jsonPatchContent).getAsJsonObject().toString(), contentType);
344+
this.bodyContent(requestGson.toJson(jsonPatchContent), contentType);
345345
} else {
346346
this.bodyContent(nonJsonContent, contentType);
347347
}

src/test/java/com/ibm/cloud/sdk/core/test/service/RequestBuilderTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
import com.ibm.cloud.sdk.core.http.HttpMediaType;
1818
import com.ibm.cloud.sdk.core.http.RequestBuilder;
1919
import com.ibm.cloud.sdk.core.test.TestUtils;
20+
import com.ibm.cloud.sdk.core.test.model.generated.Car;
21+
import com.ibm.cloud.sdk.core.test.model.generated.Truck;
22+
import com.ibm.cloud.sdk.core.test.model.generated.Vehicle;
23+
import com.ibm.cloud.sdk.core.util.GsonSingleton;
24+
2025
import okhttp3.HttpUrl;
2126
import okhttp3.MediaType;
2227
import okhttp3.Request;
@@ -26,7 +31,10 @@
2631

2732
import java.io.File;
2833
import java.io.IOException;
34+
import java.io.InputStream;
2935
import java.util.Arrays;
36+
import java.util.ArrayList;
37+
import java.util.List;
3038

3139
import static org.junit.Assert.assertEquals;
3240
import static org.junit.Assert.assertNotNull;
@@ -196,6 +204,28 @@ public void testWithContentString() throws IOException {
196204

197205
}
198206

207+
/**
208+
* Test with list of models.
209+
*
210+
* @throws IOException Signals that an I/O exception has occurred.
211+
*/
212+
@Test
213+
public void testBodyContentList() throws IOException {
214+
// add list of actual models
215+
final List<Vehicle> listOfModels = new ArrayList<>();
216+
listOfModels.add(new Truck.Builder().vehicleType("Truck").make("Ford").engineType("raptor").build());
217+
listOfModels.add(new Car.Builder().vehicleType("Car").make("Ford").bodyStyle("mach-e").build());
218+
219+
final Request request = RequestBuilder.post(HttpUrl.parse(urlWithQuery))
220+
.bodyContent("application/json", listOfModels, null, (InputStream) null).build();
221+
final RequestBody requestedBody = request.body();
222+
final Buffer buffer = new Buffer();
223+
requestedBody.writeTo(buffer);
224+
225+
assertEquals(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(listOfModels), buffer.readUtf8());
226+
assertEquals(HttpMediaType.JSON, requestedBody.contentType());
227+
}
228+
199229
/**
200230
* Test with form object array.
201231
*

0 commit comments

Comments
 (0)