fix java apache client optional body, add tests (#14227)

This commit is contained in:
William Cheng 2022-12-08 20:58:47 +08:00 committed by GitHub
parent 1fad61e2f8
commit 11d31117a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 4 deletions

View File

@ -1059,6 +1059,9 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
} else { } else {
throw new ApiException("method " + method + " does not support a request body"); throw new ApiException("method " + method + " does not support a request body");
} }
} else {
// for empty body
builder.setEntity(serialize(null, null, contentTypeObj));
} }
try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) { try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) {

View File

@ -932,6 +932,9 @@ public class ApiClient extends JavaTimeFormatter {
} else { } else {
throw new ApiException("method " + method + " does not support a request body"); throw new ApiException("method " + method + " does not support a request body");
} }
} else {
// for empty body
builder.setEntity(serialize(null, null, contentTypeObj));
} }
try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) { try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) {

View File

@ -15,10 +15,8 @@ package org.openapitools.client;
import org.junit.Assert; import org.junit.Assert;
import org.openapitools.client.ApiException; import org.openapitools.client.ApiException;
import org.openapitools.client.api.QueryApi; import org.openapitools.client.api.*;
import org.openapitools.client.model.Category; import org.openapitools.client.model.*;
import org.openapitools.client.model.Pet;
import org.openapitools.client.model.TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter;
import org.junit.Test; import org.junit.Test;
import org.junit.Ignore; import org.junit.Ignore;
@ -31,7 +29,29 @@ import java.util.*;
public class CustomTest { public class CustomTest {
private final QueryApi api = new QueryApi(); private final QueryApi api = new QueryApi();
private final BodyApi bodyApi = new BodyApi();
/**
* Test body parameter(s)
* <p>
* Test body parameter(s)
*
* @throws ApiException if the Api call fails
*/
@Test
public void testEchoBodyPet() throws ApiException {
Pet queryObject = new Pet().id(12345L).name("Hello World").
photoUrls(Arrays.asList(new String[]{"http://a.com", "http://b.com"})).category(new Category().id(987L).name("new category"));
Pet p = bodyApi.testEchoBodyPet(queryObject);
Assert.assertNotNull(p);
Assert.assertEquals("Hello World", p.getName());
Assert.assertEquals(Long.valueOf(12345L), p.getId());
// response is empty body
Pet p2 = bodyApi.testEchoBodyPet(null);
Assert.assertNull(p2);
}
/** /**
* Test query parameter(s) * Test query parameter(s)

View File

@ -1075,6 +1075,9 @@ public class ApiClient extends JavaTimeFormatter {
} else { } else {
throw new ApiException("method " + method + " does not support a request body"); throw new ApiException("method " + method + " does not support a request body");
} }
} else {
// for empty body
builder.setEntity(serialize(null, null, contentTypeObj));
} }
try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) { try (CloseableHttpResponse response = httpClient.execute(builder.build(), context)) {