forked from loafle/openapi-generator-original
[JAVA] java.net.http.HttpClient (java native) form data generator (#14554)
* java native form data generator * merge master; add test * change test * store body echo response * review fixes
This commit is contained in:
@@ -21,9 +21,21 @@ import org.openapitools.client.model.Pet;
|
||||
|
||||
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.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.Pipe;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
|
||||
@@ -20,9 +20,21 @@ import org.openapitools.client.Pair;
|
||||
|
||||
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.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.Pipe;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
@@ -143,7 +155,21 @@ public class FormApi {
|
||||
|
||||
localVarRequestBuilder.header("Accept", "text/plain");
|
||||
|
||||
localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.noBody());
|
||||
List<NameValuePair> formValues = new ArrayList<>();
|
||||
formValues.add(new BasicNameValuePair("integer_form", integerForm.toString()));
|
||||
formValues.add(new BasicNameValuePair("boolean_form", booleanForm.toString()));
|
||||
formValues.add(new BasicNameValuePair("string_form", stringForm.toString()));
|
||||
HttpEntity entity = new UrlEncodedFormEntity(formValues, java.nio.charset.StandardCharsets.UTF_8);
|
||||
ByteArrayOutputStream formOutputStream = new ByteArrayOutputStream();
|
||||
try {
|
||||
entity.writeTo(formOutputStream);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
localVarRequestBuilder
|
||||
.header("Content-Type", entity.getContentType().getValue())
|
||||
.method("POST", HttpRequest.BodyPublishers
|
||||
.ofInputStream(() -> new ByteArrayInputStream(formOutputStream.toByteArray())));
|
||||
if (memberVarReadTimeout != null) {
|
||||
localVarRequestBuilder.timeout(memberVarReadTimeout);
|
||||
}
|
||||
|
||||
@@ -20,9 +20,21 @@ import org.openapitools.client.Pair;
|
||||
|
||||
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.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.Pipe;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
|
||||
@@ -20,9 +20,21 @@ import org.openapitools.client.Pair;
|
||||
|
||||
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.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.Pipe;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
|
||||
@@ -24,9 +24,21 @@ import org.openapitools.client.model.TestQueryStyleFormExplodeTrueArrayStringQue
|
||||
|
||||
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.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.Pipe;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
|
||||
@@ -31,6 +31,7 @@ public class CustomTest {
|
||||
|
||||
private final QueryApi api = new QueryApi();
|
||||
private final BodyApi bodyApi = new BodyApi();
|
||||
private final FormApi formApi = new FormApi();
|
||||
|
||||
|
||||
/**
|
||||
@@ -245,4 +246,24 @@ public class CustomTest {
|
||||
" at [Source: (String)\"{ \"array_string_enum_default\": [\"invalid\"] }\"; line: 1, column: 33] (through reference chain: org.openapitools.client.model.DefaultValue[\"array_string_enum_default\"]->java.util.ArrayList[0])");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test form parameter(s)
|
||||
*
|
||||
* Test form parameter(s)
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
public void testFormIntegerBooleanStringTest() throws ApiException {
|
||||
Integer integerForm = 1337;
|
||||
Boolean booleanForm = true;
|
||||
String stringForm = "Hello World";
|
||||
String response = formApi.testFormIntegerBooleanString(integerForm, booleanForm, stringForm);
|
||||
org.openapitools.client.EchoServerResponseParser p = new org.openapitools.client.EchoServerResponseParser(response);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,14 +21,17 @@ public class EchoServerResponseParser {
|
||||
public String path; // e.g. /query/style_form/explode_true/object?id=12345
|
||||
public String protocol; // e.g. HTTP/1.1
|
||||
public java.util.HashMap<String, String> headers = new java.util.HashMap<>();
|
||||
public String body; // e.g. <html><head></head><body>Hello World!</body></html>
|
||||
|
||||
public EchoServerResponseParser(String response) {
|
||||
if (response == null) {
|
||||
throw new RuntimeException("Echo server response cannot be null");
|
||||
}
|
||||
|
||||
String[] lines = response.split("\n");
|
||||
Iterable<String> lines = response.lines()::iterator;
|
||||
boolean firstLine = true;
|
||||
boolean bodyStart = false;
|
||||
StringBuilder bodyBuilder = new StringBuilder();
|
||||
|
||||
for (String line : lines) {
|
||||
if (firstLine) {
|
||||
@@ -40,6 +43,16 @@ public class EchoServerResponseParser {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (bodyStart) {
|
||||
bodyBuilder.append(line);
|
||||
bodyBuilder.append("\n");
|
||||
}
|
||||
|
||||
if (line.isEmpty()) {
|
||||
bodyStart = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// store the header key-value pair in headers
|
||||
String[] keyValue = line.split(": ");
|
||||
if (keyValue.length == 2) { // skip blank line, non key-value pair
|
||||
@@ -47,5 +60,6 @@ public class EchoServerResponseParser {
|
||||
}
|
||||
}
|
||||
|
||||
body = bodyBuilder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user