[Java][okhttp-gson] Support text/plain body (#10885)

* support serialize RequestBody with contentType text/plain

* add Serialize test

* update test comment
This commit is contained in:
Kevin Chen 2021-11-26 01:04:19 -06:00 committed by GitHub
parent 06faa289bd
commit 3d92df5a41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 0 deletions

View File

@ -1081,6 +1081,8 @@ public class ApiClient {
} else if (obj instanceof File) { } else if (obj instanceof File) {
// File body parameter support. // File body parameter support.
return RequestBody.create((File) obj, MediaType.parse(contentType)); return RequestBody.create((File) obj, MediaType.parse(contentType));
} else if ("text/plain".equals(contentType) && obj instanceof String) {
return RequestBody.create((String) obj, MediaType.parse(contentType));
} else if (isJsonMime(contentType)) { } else if (isJsonMime(contentType)) {
String content; String content;
if (obj != null) { if (obj != null) {

View File

@ -871,6 +871,8 @@ public class ApiClient {
} else if (obj instanceof File) { } else if (obj instanceof File) {
// File body parameter support. // File body parameter support.
return RequestBody.create((File) obj, MediaType.parse(contentType)); return RequestBody.create((File) obj, MediaType.parse(contentType));
} else if ("text/plain".equals(contentType) && obj instanceof String) {
return RequestBody.create((String) obj, MediaType.parse(contentType));
} else if (isJsonMime(contentType)) { } else if (isJsonMime(contentType)) {
String content; String content;
if (obj != null) { if (obj != null) {

View File

@ -972,6 +972,8 @@ public class ApiClient {
} else if (obj instanceof File) { } else if (obj instanceof File) {
// File body parameter support. // File body parameter support.
return RequestBody.create((File) obj, MediaType.parse(contentType)); return RequestBody.create((File) obj, MediaType.parse(contentType));
} else if ("text/plain".equals(contentType) && obj instanceof String) {
return RequestBody.create((String) obj, MediaType.parse(contentType));
} else if (isJsonMime(contentType)) { } else if (isJsonMime(contentType)) {
String content; String content;
if (obj != null) { if (obj != null) {

View File

@ -973,6 +973,8 @@ public class ApiClient {
} else if (obj instanceof File) { } else if (obj instanceof File) {
// File body parameter support. // File body parameter support.
return RequestBody.create((File) obj, MediaType.parse(contentType)); return RequestBody.create((File) obj, MediaType.parse(contentType));
} else if ("text/plain".equals(contentType) && obj instanceof String) {
return RequestBody.create((String) obj, MediaType.parse(contentType));
} else if (isJsonMime(contentType)) { } else if (isJsonMime(contentType)) {
String content; String content;
if (obj != null) { if (obj != null) {

View File

@ -973,6 +973,8 @@ public class ApiClient {
} else if (obj instanceof File) { } else if (obj instanceof File) {
// File body parameter support. // File body parameter support.
return RequestBody.create((File) obj, MediaType.parse(contentType)); return RequestBody.create((File) obj, MediaType.parse(contentType));
} else if ("text/plain".equals(contentType) && obj instanceof String) {
return RequestBody.create((String) obj, MediaType.parse(contentType));
} else if (isJsonMime(contentType)) { } else if (isJsonMime(contentType)) {
String content; String content;
if (obj != null) { if (obj != null) {

View File

@ -342,4 +342,22 @@ public class ApiClientTest {
public void testNullHttpClient() { public void testNullHttpClient() {
apiClient.setHttpClient(null); apiClient.setHttpClient(null);
} }
/**
* Tests the ApiClient serialize methods
*/
@Test
public void testSerializeRequest() throws ApiException {
assertNotNull(apiClient.serialize("test", "text/plain"));
assertNotNull(apiClient.serialize("{}", "application/json"));
}
/**
* Tests the ApiClient serialize methods with unsupported content-type
* should raise ApiException
*/
@Test(expected = ApiException.class)
public void testUnsupportedSerializeRequest() throws ApiException {
apiClient.serialize("test", "unsupported/type");
}
} }