forked from loafle/openapi-generator-original
Fix response body in Java native client (#14222)
* fix response body in java native client * add new files * update samples
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Echo Server API
|
||||
* Echo Server API
|
||||
*
|
||||
* The version of the OpenAPI document: 0.1.0
|
||||
* Contact: team@openapitools.org
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
package org.openapitools.client.api;
|
||||
|
||||
import org.openapitools.client.ApiClient;
|
||||
import org.openapitools.client.ApiException;
|
||||
import org.openapitools.client.ApiResponse;
|
||||
import org.openapitools.client.Pair;
|
||||
|
||||
import org.openapitools.client.model.Pet;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@javax.annotation.processing.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||
public class BodyApi {
|
||||
private final HttpClient memberVarHttpClient;
|
||||
private final ObjectMapper memberVarObjectMapper;
|
||||
private final String memberVarBaseUri;
|
||||
private final Consumer<HttpRequest.Builder> memberVarInterceptor;
|
||||
private final Duration memberVarReadTimeout;
|
||||
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
|
||||
private final Consumer<HttpResponse<String>> memberVarAsyncResponseInterceptor;
|
||||
|
||||
public BodyApi() {
|
||||
this(new ApiClient());
|
||||
}
|
||||
|
||||
public BodyApi(ApiClient apiClient) {
|
||||
memberVarHttpClient = apiClient.getHttpClient();
|
||||
memberVarObjectMapper = apiClient.getObjectMapper();
|
||||
memberVarBaseUri = apiClient.getBaseUri();
|
||||
memberVarInterceptor = apiClient.getRequestInterceptor();
|
||||
memberVarReadTimeout = apiClient.getReadTimeout();
|
||||
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
|
||||
memberVarAsyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
|
||||
}
|
||||
|
||||
protected ApiException getApiException(String operationId, HttpResponse<InputStream> response) throws IOException {
|
||||
String body = response.body() == null ? null : new String(response.body().readAllBytes());
|
||||
String message = formatExceptionMessage(operationId, response.statusCode(), body);
|
||||
return new ApiException(response.statusCode(), message, response.headers(), body);
|
||||
}
|
||||
|
||||
private String formatExceptionMessage(String operationId, int statusCode, String body) {
|
||||
if (body == null || body.isEmpty()) {
|
||||
body = "[no body]";
|
||||
}
|
||||
return operationId + " call failed with: " + statusCode + " - " + body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test body parameter(s)
|
||||
* Test body parameter(s)
|
||||
* @param pet Pet object that needs to be added to the store (optional)
|
||||
* @return Pet
|
||||
* @throws ApiException if fails to make API call
|
||||
*/
|
||||
public Pet testEchoBodyPet(Pet pet) throws ApiException {
|
||||
ApiResponse<Pet> localVarResponse = testEchoBodyPetWithHttpInfo(pet);
|
||||
return localVarResponse.getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test body parameter(s)
|
||||
* Test body parameter(s)
|
||||
* @param pet Pet object that needs to be added to the store (optional)
|
||||
* @return ApiResponse<Pet>
|
||||
* @throws ApiException if fails to make API call
|
||||
*/
|
||||
public ApiResponse<Pet> testEchoBodyPetWithHttpInfo(Pet pet) throws ApiException {
|
||||
HttpRequest.Builder localVarRequestBuilder = testEchoBodyPetRequestBuilder(pet);
|
||||
try {
|
||||
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
|
||||
localVarRequestBuilder.build(),
|
||||
HttpResponse.BodyHandlers.ofInputStream());
|
||||
if (memberVarResponseInterceptor != null) {
|
||||
memberVarResponseInterceptor.accept(localVarResponse);
|
||||
}
|
||||
try {
|
||||
if (localVarResponse.statusCode()/ 100 != 2) {
|
||||
throw getApiException("testEchoBodyPet", localVarResponse);
|
||||
}
|
||||
return new ApiResponse<Pet>(
|
||||
localVarResponse.statusCode(),
|
||||
localVarResponse.headers().map(),
|
||||
localVarResponse.body() == null ? null : memberVarObjectMapper.readValue(localVarResponse.body(), new TypeReference<Pet>() {}) // closes the InputStream
|
||||
);
|
||||
} finally {
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new ApiException(e);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new ApiException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpRequest.Builder testEchoBodyPetRequestBuilder(Pet pet) throws ApiException {
|
||||
|
||||
HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();
|
||||
|
||||
String localVarPath = "/echo/body/Pet";
|
||||
|
||||
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
|
||||
|
||||
localVarRequestBuilder.header("Content-Type", "application/json");
|
||||
localVarRequestBuilder.header("Accept", "application/json");
|
||||
|
||||
try {
|
||||
byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(pet);
|
||||
localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
|
||||
} catch (IOException e) {
|
||||
throw new ApiException(e);
|
||||
}
|
||||
if (memberVarReadTimeout != null) {
|
||||
localVarRequestBuilder.timeout(memberVarReadTimeout);
|
||||
}
|
||||
if (memberVarInterceptor != null) {
|
||||
memberVarInterceptor.accept(localVarRequestBuilder);
|
||||
}
|
||||
return localVarRequestBuilder;
|
||||
}
|
||||
}
|
||||
@@ -108,10 +108,9 @@ public class PathApi {
|
||||
throw getApiException("testsPathStringPathStringIntegerPathInteger", localVarResponse);
|
||||
}
|
||||
// for plain text response
|
||||
InputStream responseBody = localVarResponse.body();
|
||||
if (localVarResponse.headers().map().containsKey("Content-Type") &&
|
||||
"text/plain".equalsIgnoreCase(localVarResponse.headers().map().get("Content-Type").get(0))) {
|
||||
java.util.Scanner s = new java.util.Scanner(responseBody).useDelimiter("\\A");
|
||||
java.util.Scanner s = new java.util.Scanner(localVarResponse.body()).useDelimiter("\\A");
|
||||
String responseBodyText = s.hasNext() ? s.next() : "";
|
||||
return new ApiResponse<String>(
|
||||
localVarResponse.statusCode(),
|
||||
|
||||
@@ -112,10 +112,9 @@ public class QueryApi {
|
||||
throw getApiException("testQueryIntegerBooleanString", localVarResponse);
|
||||
}
|
||||
// for plain text response
|
||||
InputStream responseBody = localVarResponse.body();
|
||||
if (localVarResponse.headers().map().containsKey("Content-Type") &&
|
||||
"text/plain".equalsIgnoreCase(localVarResponse.headers().map().get("Content-Type").get(0))) {
|
||||
java.util.Scanner s = new java.util.Scanner(responseBody).useDelimiter("\\A");
|
||||
java.util.Scanner s = new java.util.Scanner(localVarResponse.body()).useDelimiter("\\A");
|
||||
String responseBodyText = s.hasNext() ? s.next() : "";
|
||||
return new ApiResponse<String>(
|
||||
localVarResponse.statusCode(),
|
||||
@@ -199,10 +198,9 @@ public class QueryApi {
|
||||
throw getApiException("testQueryStyleFormExplodeTrueArrayString", localVarResponse);
|
||||
}
|
||||
// for plain text response
|
||||
InputStream responseBody = localVarResponse.body();
|
||||
if (localVarResponse.headers().map().containsKey("Content-Type") &&
|
||||
"text/plain".equalsIgnoreCase(localVarResponse.headers().map().get("Content-Type").get(0))) {
|
||||
java.util.Scanner s = new java.util.Scanner(responseBody).useDelimiter("\\A");
|
||||
java.util.Scanner s = new java.util.Scanner(localVarResponse.body()).useDelimiter("\\A");
|
||||
String responseBodyText = s.hasNext() ? s.next() : "";
|
||||
return new ApiResponse<String>(
|
||||
localVarResponse.statusCode(),
|
||||
@@ -284,10 +282,9 @@ public class QueryApi {
|
||||
throw getApiException("testQueryStyleFormExplodeTrueObject", localVarResponse);
|
||||
}
|
||||
// for plain text response
|
||||
InputStream responseBody = localVarResponse.body();
|
||||
if (localVarResponse.headers().map().containsKey("Content-Type") &&
|
||||
"text/plain".equalsIgnoreCase(localVarResponse.headers().map().get("Content-Type").get(0))) {
|
||||
java.util.Scanner s = new java.util.Scanner(responseBody).useDelimiter("\\A");
|
||||
java.util.Scanner s = new java.util.Scanner(localVarResponse.body()).useDelimiter("\\A");
|
||||
String responseBodyText = s.hasNext() ? s.next() : "";
|
||||
return new ApiResponse<String>(
|
||||
localVarResponse.statusCode(),
|
||||
|
||||
@@ -15,10 +15,8 @@ package org.openapitools.client;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.openapitools.client.ApiException;
|
||||
import org.openapitools.client.api.QueryApi;
|
||||
import org.openapitools.client.model.Category;
|
||||
import org.openapitools.client.model.Pet;
|
||||
import org.openapitools.client.model.TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter;
|
||||
import org.openapitools.client.api.*;
|
||||
import org.openapitools.client.model.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.Ignore;
|
||||
|
||||
@@ -31,8 +29,31 @@ import java.util.*;
|
||||
public class CustomTest {
|
||||
|
||||
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)
|
||||
* <p>
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Echo Server API
|
||||
* Echo Server API
|
||||
*
|
||||
* The version of the OpenAPI document: 0.1.0
|
||||
* Contact: team@openapitools.org
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
|
||||
package org.openapitools.client.api;
|
||||
|
||||
import org.openapitools.client.ApiException;
|
||||
import org.openapitools.client.model.Pet;
|
||||
import org.junit.Test;
|
||||
import org.junit.Ignore;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
/**
|
||||
* API tests for BodyApi
|
||||
*/
|
||||
@Ignore
|
||||
public class BodyApiTest {
|
||||
|
||||
private final BodyApi api = new BodyApi();
|
||||
|
||||
|
||||
/**
|
||||
* Test body parameter(s)
|
||||
*
|
||||
* Test body parameter(s)
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
public void testEchoBodyPetTest() throws ApiException {
|
||||
Pet pet = null;
|
||||
Pet response =
|
||||
api.testEchoBodyPet(pet);
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user