Skip to content

Commit 8978a0a

Browse files
committed
Merge pull request #1242 from xhh/options-http-method
[Java okhttp-gson] Support the "OPTIONS" HTTP method
2 parents 6d21422 + 477d5c4 commit 8978a0a

File tree

2 files changed

+8
-48
lines changed
  • modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson
  • samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client

2 files changed

+8
-48
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

samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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

@@ -697,7 +698,7 @@ public <T> T handleResponse(Response response, Type returnType) throws ApiExcept
697698
* Build HTTP call with the given options.
698699
*
699700
* @param path The sub-path of the HTTP URL
700-
* @param method The request method, one of "GET", "HEAD", "POST", "PUT", "PATCH" and "DELETE"
701+
* @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE"
701702
* @param queryParams The query parameters
702703
* @param body The request body object
703704
* @param headerParams The header parameters
@@ -717,7 +718,7 @@ public Call buildCall(String path, String method, List<Pair> queryParams, Object
717718
if (contentType == null) contentType = "application/json";
718719

719720
RequestBody reqBody;
720-
if ("GET".equals(method) || "HEAD".equals(method)) {
721+
if (!HttpMethod.permitsRequestBody(method)) {
721722
reqBody = null;
722723
} else if ("application/x-www-form-urlencoded".equals(contentType)) {
723724
reqBody = buildRequestBodyFormEncoding(formParams);
@@ -735,28 +736,7 @@ public Call buildCall(String path, String method, List<Pair> queryParams, Object
735736
reqBody = RequestBody.create(MediaType.parse(contentType), serialize(body, contentType));
736737
}
737738

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

0 commit comments

Comments
 (0)