Skip to content

Commit 925ec01

Browse files
committed
side effect of #362. Should also bypass object mapper when dealing with native json types
1 parent 664425f commit 925ec01

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

unirest/src/main/java/kong/unirest/HttpRequestBody.java

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package kong.unirest;
2727

2828
import kong.unirest.json.JSONArray;
29+
import kong.unirest.json.JSONElement;
2930
import kong.unirest.json.JSONObject;
3031

3132
import java.io.File;
@@ -109,6 +110,10 @@ public RequestBodyEntity body(String body) {
109110
public RequestBodyEntity body(Object body) {
110111
if(body instanceof String){
111112
return body((String)body);
113+
} else if (body instanceof JsonNode){
114+
return body((JsonNode) body);
115+
} else if (body instanceof JSONElement){
116+
return body(body.toString());
112117
}
113118
return body(getObjectMapper().writeValue(body));
114119
}

unirest/src/test/java/BehaviorTests/UniBodyPostingTest.java

+40
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,44 @@ void stringPassedToObjectGetsPassedToString() {
189189
Object value = request.getBody().get().uniPart().getValue();
190190
assertEquals("{\"body\": \"sample\"}", value);
191191
}
192+
193+
@Test
194+
void jsonNodePassedToObjectGetsPassedToString() {
195+
Object body = new JsonNode("{\"body\": \"sample\"}");
196+
RequestBodyEntity request = Unirest.post(MockServer.POST)
197+
.basicAuth("foo", "bar")
198+
.header("Content-Type", "application/json")
199+
.queryString("foo", "bar")
200+
.body(body);
201+
202+
Object value = request.getBody().get().uniPart().getValue();
203+
assertEquals("{\"body\":\"sample\"}", value);
204+
}
205+
206+
@Test
207+
void jsonObjectPassedToObjectGetsPassedToString() {
208+
Object body = new JSONObject("{\"body\": \"sample\"}");
209+
RequestBodyEntity request = Unirest.post(MockServer.POST)
210+
.basicAuth("foo", "bar")
211+
.header("Content-Type", "application/json")
212+
.queryString("foo", "bar")
213+
.body(body);
214+
215+
Object value = request.getBody().get().uniPart().getValue();
216+
assertEquals("{\"body\":\"sample\"}", value);
217+
}
218+
219+
@Test
220+
void jsonArrayPassedToObjectGetsPassedToString() {
221+
Object body = new JSONArray("[\"body\", \"sample\"]");
222+
RequestBodyEntity request = Unirest.post(MockServer.POST)
223+
.basicAuth("foo", "bar")
224+
.header("Content-Type", "application/json")
225+
.queryString("foo", "bar")
226+
.body(body);
227+
228+
Object value = request.getBody().get().uniPart().getValue();
229+
assertEquals("[\"body\",\"sample\"]", value);
230+
}
231+
192232
}

0 commit comments

Comments
 (0)