forked from loafle/openapi-generator-original
* [Java] Fixed bug in native client generation when API accepts array of files (#16055) * Adding test for java native client * Updated samples
This commit is contained in:
@@ -24,6 +24,12 @@ import org.openapitools.client.model.Tag;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -229,6 +235,120 @@ public class BodyApi {
|
||||
}
|
||||
return localVarRequestBuilder;
|
||||
}
|
||||
/**
|
||||
* Test array of binary in multipart mime
|
||||
* Test array of binary in multipart mime
|
||||
* @param files (required)
|
||||
* @return String
|
||||
* @throws ApiException if fails to make API call
|
||||
*/
|
||||
public String testBodyMultipartFormdataArrayOfBinary(List<File> files) throws ApiException {
|
||||
ApiResponse<String> localVarResponse = testBodyMultipartFormdataArrayOfBinaryWithHttpInfo(files);
|
||||
return localVarResponse.getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test array of binary in multipart mime
|
||||
* Test array of binary in multipart mime
|
||||
* @param files (required)
|
||||
* @return ApiResponse<String>
|
||||
* @throws ApiException if fails to make API call
|
||||
*/
|
||||
public ApiResponse<String> testBodyMultipartFormdataArrayOfBinaryWithHttpInfo(List<File> files) throws ApiException {
|
||||
HttpRequest.Builder localVarRequestBuilder = testBodyMultipartFormdataArrayOfBinaryRequestBuilder(files);
|
||||
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("testBodyMultipartFormdataArrayOfBinary", localVarResponse);
|
||||
}
|
||||
// for plain text response
|
||||
if (localVarResponse.headers().map().containsKey("Content-Type") &&
|
||||
"text/plain".equalsIgnoreCase(localVarResponse.headers().map().get("Content-Type").get(0).split(";")[0].trim())) {
|
||||
java.util.Scanner s = new java.util.Scanner(localVarResponse.body()).useDelimiter("\\A");
|
||||
String responseBodyText = s.hasNext() ? s.next() : "";
|
||||
return new ApiResponse<String>(
|
||||
localVarResponse.statusCode(),
|
||||
localVarResponse.headers().map(),
|
||||
responseBodyText
|
||||
);
|
||||
} else {
|
||||
throw new RuntimeException("Error! The response Content-Type is supposed to be `text/plain` but it's not: " + localVarResponse);
|
||||
}
|
||||
} finally {
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new ApiException(e);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new ApiException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpRequest.Builder testBodyMultipartFormdataArrayOfBinaryRequestBuilder(List<File> files) throws ApiException {
|
||||
// verify the required parameter 'files' is set
|
||||
if (files == null) {
|
||||
throw new ApiException(400, "Missing the required parameter 'files' when calling testBodyMultipartFormdataArrayOfBinary");
|
||||
}
|
||||
|
||||
HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();
|
||||
|
||||
String localVarPath = "/body/application/octetstream/array_of_binary";
|
||||
|
||||
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
|
||||
|
||||
localVarRequestBuilder.header("Accept", "text/plain");
|
||||
|
||||
MultipartEntityBuilder multiPartBuilder = MultipartEntityBuilder.create();
|
||||
boolean hasFiles = false;
|
||||
for (int i=0; i < files.size(); i++) {
|
||||
multiPartBuilder.addBinaryBody("files", files.get(i));
|
||||
hasFiles = true;
|
||||
}
|
||||
HttpEntity entity = multiPartBuilder.build();
|
||||
HttpRequest.BodyPublisher formDataPublisher;
|
||||
if (hasFiles) {
|
||||
Pipe pipe;
|
||||
try {
|
||||
pipe = Pipe.open();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
new Thread(() -> {
|
||||
try (OutputStream outputStream = Channels.newOutputStream(pipe.sink())) {
|
||||
entity.writeTo(outputStream);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}).start();
|
||||
formDataPublisher = HttpRequest.BodyPublishers.ofInputStream(() -> Channels.newInputStream(pipe.source()));
|
||||
} else {
|
||||
ByteArrayOutputStream formOutputStream = new ByteArrayOutputStream();
|
||||
try {
|
||||
entity.writeTo(formOutputStream);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
formDataPublisher = HttpRequest.BodyPublishers
|
||||
.ofInputStream(() -> new ByteArrayInputStream(formOutputStream.toByteArray()));
|
||||
}
|
||||
localVarRequestBuilder
|
||||
.header("Content-Type", entity.getContentType().getValue())
|
||||
.method("POST", formDataPublisher);
|
||||
if (memberVarReadTimeout != null) {
|
||||
localVarRequestBuilder.timeout(memberVarReadTimeout);
|
||||
}
|
||||
if (memberVarInterceptor != null) {
|
||||
memberVarInterceptor.accept(localVarRequestBuilder);
|
||||
}
|
||||
return localVarRequestBuilder;
|
||||
}
|
||||
/**
|
||||
* Test free form object
|
||||
* Test free form object
|
||||
|
||||
@@ -21,7 +21,11 @@ import org.openapitools.client.model.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.Ignore;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -279,4 +283,33 @@ public class CustomTest {
|
||||
Assert.assertEquals("/form/integer/boolean/string", p.path);
|
||||
Assert.assertEquals("3b\ninteger_form=1337&boolean_form=true&string_form=Hello+World\n0\n\n", p.body);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBodyMultipartFormdataArrayOfBinary() throws ApiException {
|
||||
File file1 = Objects.requireNonNull(getFile("Hello"));
|
||||
File file2 = Objects.requireNonNull(getFile("World"));
|
||||
|
||||
String response = bodyApi.testBodyMultipartFormdataArrayOfBinary(List.of(file1, file2));
|
||||
org.openapitools.client.EchoServerResponseParser p = new org.openapitools.client.EchoServerResponseParser(response);
|
||||
|
||||
Assert.assertEquals("/body/application/octetstream/array_of_binary", p.path);
|
||||
|
||||
Assert.assertTrue(p.body.contains(file1.getName()));
|
||||
Assert.assertTrue(p.body.contains("Hello"));
|
||||
Assert.assertTrue(p.body.contains(file2.getName()));
|
||||
Assert.assertTrue(p.body.contains("World"));
|
||||
}
|
||||
|
||||
private File getFile(String content) {
|
||||
try {
|
||||
File tempFile = Files.createTempFile("tempFile", ".txt").toFile();
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
|
||||
writer.write(content);
|
||||
writer.close();
|
||||
|
||||
return tempFile;
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user