Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle OkHttp POST with empty body #109

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private FunctionsByCategory getFunctions(ReturnType returnType) {
}

private Request convertToOkHttpRequest(RequestData request) {
var requestBody = createRequestBody(request.getBody(), request.getContentType());
var requestBody = createRequestBody(request.getBody(), request.getContentType(), request.getHttpMethod());
var headersArray = request.getHeaders().toArray(new String[0]);
var requestBuilder = new Request.Builder()
.url(request.getUrl())
Expand All @@ -152,10 +152,13 @@ private Request convertToOkHttpRequest(RequestData request) {
}

@SuppressWarnings("unchecked")
private RequestBody createRequestBody(Object bodyObject, ContentType contentType) {
private RequestBody createRequestBody(Object bodyObject, ContentType contentType, String httpMethod) {
RequestBody requestBody = null;
if (contentType == null) {
logger.debug(REQUEST_BODY_FORMAT, "(Empty)");
if (httpMethod.equalsIgnoreCase("POST")) {
requestBody = RequestBody.create("", null);
}
} else if (contentType == ContentType.MULTIPART_FORMDATA) {
logger.debug(REQUEST_BODY_FORMAT, bodyObject);
var bodyBytes = HttpMultipart.toByteArrays((Map<String, Object>) bodyObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,28 @@ default void shouldShutdownWithoutExceptions() {
testShutdown();
}

@Test
default void shouldReturnAnObjectSyncWhenIsPostMethodAndBodyIsEmpty() throws IOException, InterruptedException {
setMocksForString(SyncType.SYNC, "{\"id\":100,\"description\":\"Description\",\"active\":true}");

var service = getHttpProcessor().createProxy(ITest.SyncService.class);
var actualDemo = service.cancelDemo(100);
var expectedDemo = new ITest.Demo(100, "Description", true);

assertEquals(expectedDemo, actualDemo);
}

@Test
default void shouldReturnAnObjectAsyncWhenIsPostMethodAndBodyIsEmpty() throws IOException, InterruptedException {
setMocksForString(SyncType.ASYNC, "{\"id\":100,\"description\":\"Description\",\"active\":true}");

var service = getHttpProcessor().createProxy(ITest.AsyncService.class);
var actualDemo = service.cancelDemo(100).join();
var expectedDemo = new ITest.Demo(100, "Description", true);

assertEquals(expectedDemo, actualDemo);
}

private String transformUsers(String jsonInput) {
List<String> flatUsers = new ArrayList<>();
String patternStr = "\"id\":\\s*(\\d+).*?" + // id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ interface AsyncService {
@GET("/{demoId}")
CompletableFuture<Demo> getDemo(@Path("demoId") Integer demoId);

@POST
CompletableFuture<Demo> cancelDemo(@Path("demoId") Integer demoId);

@GET("/{genericDemoId}")
CompletableFuture<Generic<Demo>> getGenericDemo(@Path("genericDemoId") Integer genericDemoId);

Expand Down Expand Up @@ -113,6 +116,9 @@ interface SyncService {
@GET("/{demoId}")
Demo getDemo(@Path("demoId") Integer demoId);

@POST
Demo cancelDemo(@Path("demoId") Integer demoId);

@GET("/{genericDemoId}")
Generic<Demo> getGenericDemo(@Path("genericDemoId") Integer genericDemoId);

Expand Down
Loading