Skip to content

Commit 460c8ca

Browse files
committed
Support OPTIONS in Java okhttp-gson client
1 parent 53e13ae commit 460c8ca

File tree

1 file changed

+4
-24
lines changed

1 file changed

+4
-24
lines changed

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.squareup.okhttp.FormEncodingBuilder;
1010
import com.squareup.okhttp.MultipartBuilder;
1111
import com.squareup.okhttp.MediaType;
1212
import com.squareup.okhttp.Headers;
13+
import com.squareup.okhttp.internal.http.HttpMethod;
1314

1415
import java.lang.reflect.Type;
1516

@@ -698,7 +699,7 @@ public class ApiClient {
698699
* Build HTTP call with the given options.
699700
*
700701
* @param path The sub-path of the HTTP URL
701-
* @param method The request method, one of "GET", "HEAD", "POST", "PUT", "PATCH" and "DELETE"
702+
* @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE"
702703
* @param queryParams The query parameters
703704
* @param body The request body object
704705
* @param headerParams The header parameters
@@ -718,7 +719,7 @@ public class ApiClient {
718719
if (contentType == null) contentType = "application/json";
719720
720721
RequestBody reqBody;
721-
if ("GET".equals(method) || "HEAD".equals(method)) {
722+
if (!HttpMethod.permitsRequestBody(method)) {
722723
reqBody = null;
723724
} else if ("application/x-www-form-urlencoded".equals(contentType)) {
724725
reqBody = buildRequestBodyFormEncoding(formParams);
@@ -736,28 +737,7 @@ public class ApiClient {
736737
reqBody = RequestBody.create(MediaType.parse(contentType), serialize(body, contentType));
737738
}
738739

739-
Request request;
740-
if ("GET".equals(method)) {
741-
request = reqBuilder.get().build();
742-
} else if ("HEAD".equals(method)) {
743-
request = reqBuilder.head().build();
744-
} else if ("POST".equals(method)) {
745-
request = reqBuilder.post(reqBody).build();
746-
} else if ("PUT".equals(method)) {
747-
request = reqBuilder.put(reqBody).build();
748-
} else if ("PATCH".equals(method)) {
749-
request = reqBuilder.patch(reqBody).build();
750-
} else if ("DELETE".equals(method)) {
751-
if (reqBody == null) {
752-
// calling DELETE without sending a request body
753-
request = reqBuilder.delete().build();
754-
} else {
755-
request = reqBuilder.delete(reqBody).build();
756-
}
757-
} else {
758-
throw new ApiException("unknown method type: " + method);
759-
}
760-
740+
Request request = reqBuilder.method(method, reqBody).build();
761741
return httpClient.newCall(request);
762742
}
763743

0 commit comments

Comments
 (0)