mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-04 08:56:07 +00:00
[java][apache-httpclient] Add deep object url query parameter support (#14417)
* add deep object url query parameter to java apache-httpclient * update doc string
This commit is contained in:
@@ -766,9 +766,10 @@ public class ApiClient extends JavaTimeFormatter {
|
||||
* @param path The sub path
|
||||
* @param queryParams The query parameters
|
||||
* @param collectionQueryParams The collection query parameters
|
||||
* @param urlQueryDeepObject URL query string of the deep object parameters
|
||||
* @return The full URL
|
||||
*/
|
||||
private String buildUrl(String path, List<Pair> queryParams, List<Pair> collectionQueryParams) {
|
||||
private String buildUrl(String path, List<Pair> queryParams, List<Pair> collectionQueryParams, String urlQueryDeepObject) {
|
||||
String baseURL;
|
||||
if (serverIndex != null) {
|
||||
if (serverIndex < 0 || serverIndex >= servers.size()) {
|
||||
@@ -819,6 +820,11 @@ public class ApiClient extends JavaTimeFormatter {
|
||||
}
|
||||
}
|
||||
|
||||
if (urlQueryDeepObject != null && urlQueryDeepObject.length() > 0) {
|
||||
url.append(url.toString().contains("?") ? "&" : "?");
|
||||
url.append(urlQueryDeepObject);
|
||||
}
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
@@ -860,6 +866,7 @@ public class ApiClient extends JavaTimeFormatter {
|
||||
* @param method The request method, one of "GET", "POST", "PUT", and "DELETE"
|
||||
* @param queryParams The query parameters
|
||||
* @param collectionQueryParams The collection query parameters
|
||||
* @param urlQueryDeepObject A URL query string for deep object parameters
|
||||
* @param body The request body object - if it is not binary, otherwise null
|
||||
* @param headerParams The header parameters
|
||||
* @param cookieParams The cookie parameters
|
||||
@@ -876,6 +883,7 @@ public class ApiClient extends JavaTimeFormatter {
|
||||
String method,
|
||||
List<Pair> queryParams,
|
||||
List<Pair> collectionQueryParams,
|
||||
String urlQueryDeepObject,
|
||||
Object body,
|
||||
Map<String, String> headerParams,
|
||||
Map<String, String> cookieParams,
|
||||
@@ -889,7 +897,7 @@ public class ApiClient extends JavaTimeFormatter {
|
||||
}
|
||||
|
||||
updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);
|
||||
final String url = buildUrl(path, queryParams, collectionQueryParams);
|
||||
final String url = buildUrl(path, queryParams, collectionQueryParams, urlQueryDeepObject);
|
||||
|
||||
RequestBuilder builder = RequestBuilder.create(method);
|
||||
builder.setUri(url);
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||
public class BodyApi {
|
||||
@@ -61,7 +62,8 @@ public class BodyApi {
|
||||
// create path and map variables
|
||||
String localVarPath = "/echo/body/Pet";
|
||||
|
||||
// query params
|
||||
StringJoiner localVarQueryDeepObjectStringJoiner = new StringJoiner("&");
|
||||
String localVarQueryParameterBaseName;
|
||||
List<Pair> localVarQueryParams = new ArrayList<Pair>();
|
||||
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
|
||||
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
|
||||
@@ -89,6 +91,7 @@ public class BodyApi {
|
||||
"POST",
|
||||
localVarQueryParams,
|
||||
localVarCollectionQueryParams,
|
||||
localVarQueryDeepObjectStringJoiner.toString(),
|
||||
localVarPostBody,
|
||||
localVarHeaderParams,
|
||||
localVarCookieParams,
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||
public class PathApi {
|
||||
@@ -73,7 +74,8 @@ public class PathApi {
|
||||
.replaceAll("\\{" + "path_string" + "\\}", apiClient.escapeString(pathString.toString()))
|
||||
.replaceAll("\\{" + "path_integer" + "\\}", apiClient.escapeString(pathInteger.toString()));
|
||||
|
||||
// query params
|
||||
StringJoiner localVarQueryDeepObjectStringJoiner = new StringJoiner("&");
|
||||
String localVarQueryParameterBaseName;
|
||||
List<Pair> localVarQueryParams = new ArrayList<Pair>();
|
||||
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
|
||||
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
|
||||
@@ -101,6 +103,7 @@ public class PathApi {
|
||||
"GET",
|
||||
localVarQueryParams,
|
||||
localVarCollectionQueryParams,
|
||||
localVarQueryDeepObjectStringJoiner.toString(),
|
||||
localVarPostBody,
|
||||
localVarHeaderParams,
|
||||
localVarCookieParams,
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
|
||||
public class QueryApi {
|
||||
@@ -64,7 +65,8 @@ public class QueryApi {
|
||||
// create path and map variables
|
||||
String localVarPath = "/query/integer/boolean/string";
|
||||
|
||||
// query params
|
||||
StringJoiner localVarQueryDeepObjectStringJoiner = new StringJoiner("&");
|
||||
String localVarQueryParameterBaseName;
|
||||
List<Pair> localVarQueryParams = new ArrayList<Pair>();
|
||||
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
|
||||
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
|
||||
@@ -95,6 +97,7 @@ public class QueryApi {
|
||||
"GET",
|
||||
localVarQueryParams,
|
||||
localVarCollectionQueryParams,
|
||||
localVarQueryDeepObjectStringJoiner.toString(),
|
||||
localVarPostBody,
|
||||
localVarHeaderParams,
|
||||
localVarCookieParams,
|
||||
@@ -118,14 +121,16 @@ public class QueryApi {
|
||||
// create path and map variables
|
||||
String localVarPath = "/query/style_deepObject/explode_true/object";
|
||||
|
||||
// query params
|
||||
StringJoiner localVarQueryDeepObjectStringJoiner = new StringJoiner("&");
|
||||
String localVarQueryParameterBaseName;
|
||||
List<Pair> localVarQueryParams = new ArrayList<Pair>();
|
||||
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
|
||||
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
|
||||
Map<String, String> localVarCookieParams = new HashMap<String, String>();
|
||||
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
|
||||
|
||||
localVarQueryParams.addAll(apiClient.parameterToPair("query_object", queryObject));
|
||||
localVarQueryParameterBaseName = "query_object";
|
||||
localVarQueryDeepObjectStringJoiner.add(queryObject.toUrlQueryString("query_object"));
|
||||
|
||||
|
||||
|
||||
@@ -147,6 +152,7 @@ public class QueryApi {
|
||||
"GET",
|
||||
localVarQueryParams,
|
||||
localVarCollectionQueryParams,
|
||||
localVarQueryDeepObjectStringJoiner.toString(),
|
||||
localVarPostBody,
|
||||
localVarHeaderParams,
|
||||
localVarCookieParams,
|
||||
@@ -170,7 +176,8 @@ public class QueryApi {
|
||||
// create path and map variables
|
||||
String localVarPath = "/query/style_form/explode_true/array_string";
|
||||
|
||||
// query params
|
||||
StringJoiner localVarQueryDeepObjectStringJoiner = new StringJoiner("&");
|
||||
String localVarQueryParameterBaseName;
|
||||
List<Pair> localVarQueryParams = new ArrayList<Pair>();
|
||||
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
|
||||
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
|
||||
@@ -199,6 +206,7 @@ public class QueryApi {
|
||||
"GET",
|
||||
localVarQueryParams,
|
||||
localVarCollectionQueryParams,
|
||||
localVarQueryDeepObjectStringJoiner.toString(),
|
||||
localVarPostBody,
|
||||
localVarHeaderParams,
|
||||
localVarCookieParams,
|
||||
@@ -222,7 +230,8 @@ public class QueryApi {
|
||||
// create path and map variables
|
||||
String localVarPath = "/query/style_form/explode_true/object";
|
||||
|
||||
// query params
|
||||
StringJoiner localVarQueryDeepObjectStringJoiner = new StringJoiner("&");
|
||||
String localVarQueryParameterBaseName;
|
||||
List<Pair> localVarQueryParams = new ArrayList<Pair>();
|
||||
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
|
||||
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
|
||||
@@ -256,6 +265,7 @@ public class QueryApi {
|
||||
"GET",
|
||||
localVarQueryParams,
|
||||
localVarCollectionQueryParams,
|
||||
localVarQueryDeepObjectStringJoiner.toString(),
|
||||
localVarPostBody,
|
||||
localVarHeaderParams,
|
||||
localVarCookieParams,
|
||||
|
||||
@@ -22,6 +22,9 @@ import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* Category
|
||||
@@ -132,5 +135,27 @@ public class Category {
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the instance into URL query string.
|
||||
*
|
||||
* @param prefix prefix of the query string
|
||||
* @return URL query string
|
||||
*/
|
||||
public String toUrlQueryString(String prefix) {
|
||||
if (prefix == null) {
|
||||
prefix = "";
|
||||
}
|
||||
StringJoiner joiner = new StringJoiner("&");
|
||||
// add `id` to the URL query string
|
||||
if (getId() != null) {
|
||||
joiner.add(String.format("%s[id]=%s", prefix, URLEncoder.encode(String.valueOf(getId()), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
|
||||
}
|
||||
// add `name` to the URL query string
|
||||
if (getName() != null) {
|
||||
joiner.add(String.format("%s[name]=%s", prefix, URLEncoder.encode(String.valueOf(getName()), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
|
||||
}
|
||||
return joiner.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,9 @@ import org.openapitools.client.model.Category;
|
||||
import org.openapitools.client.model.Tag;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* Pet
|
||||
@@ -314,5 +317,49 @@ public class Pet {
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the instance into URL query string.
|
||||
*
|
||||
* @param prefix prefix of the query string
|
||||
* @return URL query string
|
||||
*/
|
||||
public String toUrlQueryString(String prefix) {
|
||||
if (prefix == null) {
|
||||
prefix = "";
|
||||
}
|
||||
StringJoiner joiner = new StringJoiner("&");
|
||||
// add `id` to the URL query string
|
||||
if (getId() != null) {
|
||||
joiner.add(String.format("%s[id]=%s", prefix, URLEncoder.encode(String.valueOf(getId()), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
|
||||
}
|
||||
// add `name` to the URL query string
|
||||
if (getName() != null) {
|
||||
joiner.add(String.format("%s[name]=%s", prefix, URLEncoder.encode(String.valueOf(getName()), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
|
||||
}
|
||||
// add `category` to the URL query string
|
||||
if (getCategory() != null) {
|
||||
joiner.add(getCategory().toUrlQueryString(prefix + "[category]"));
|
||||
}
|
||||
// add `photoUrls` to the URL query string
|
||||
if (getPhotoUrls() != null) {
|
||||
for (int i = 0; i < getPhotoUrls().size(); i++) {
|
||||
joiner.add(String.format("%s[photoUrls][%d]=%s", prefix, i, URLEncoder.encode(String.valueOf(getPhotoUrls().get(i)), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
|
||||
}
|
||||
}
|
||||
// add `tags` to the URL query string
|
||||
if (getTags() != null) {
|
||||
for (int i = 0; i < getTags().size(); i++) {
|
||||
if (getTags().get(i) != null) {
|
||||
joiner.add(getTags().get(i).toUrlQueryString(String.format("%s[tags][%d]", prefix, i)));
|
||||
}
|
||||
}
|
||||
}
|
||||
// add `status` to the URL query string
|
||||
if (getStatus() != null) {
|
||||
joiner.add(String.format("%s[status]=%s", prefix, URLEncoder.encode(String.valueOf(getStatus()), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
|
||||
}
|
||||
return joiner.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,9 @@ import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* Tag
|
||||
@@ -132,5 +135,27 @@ public class Tag {
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the instance into URL query string.
|
||||
*
|
||||
* @param prefix prefix of the query string
|
||||
* @return URL query string
|
||||
*/
|
||||
public String toUrlQueryString(String prefix) {
|
||||
if (prefix == null) {
|
||||
prefix = "";
|
||||
}
|
||||
StringJoiner joiner = new StringJoiner("&");
|
||||
// add `id` to the URL query string
|
||||
if (getId() != null) {
|
||||
joiner.add(String.format("%s[id]=%s", prefix, URLEncoder.encode(String.valueOf(getId()), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
|
||||
}
|
||||
// add `name` to the URL query string
|
||||
if (getName() != null) {
|
||||
joiner.add(String.format("%s[name]=%s", prefix, URLEncoder.encode(String.valueOf(getName()), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
|
||||
}
|
||||
return joiner.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,9 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter
|
||||
@@ -111,5 +114,25 @@ public class TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter {
|
||||
return o.toString().replace("\n", "\n ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the instance into URL query string.
|
||||
*
|
||||
* @param prefix prefix of the query string
|
||||
* @return URL query string
|
||||
*/
|
||||
public String toUrlQueryString(String prefix) {
|
||||
if (prefix == null) {
|
||||
prefix = "";
|
||||
}
|
||||
StringJoiner joiner = new StringJoiner("&");
|
||||
// add `values` to the URL query string
|
||||
if (getValues() != null) {
|
||||
for (int i = 0; i < getValues().size(); i++) {
|
||||
joiner.add(String.format("%s[values][%d]=%s", prefix, i, URLEncoder.encode(String.valueOf(getValues().get(i)), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
|
||||
}
|
||||
}
|
||||
return joiner.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -65,10 +65,28 @@ public class CustomTest {
|
||||
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"));
|
||||
|
||||
// TODO uncomment below to test deepObject
|
||||
//String response = api.testQueryStyleFormExplodeTrueObject(queryObject);
|
||||
//org.openapitools.client.EchoServerResponseParser p = new org.openapitools.client.EchoServerResponseParser(response);
|
||||
//Assert.assertEquals("/query/style_form/explode_true/object?id=12345&name=Hello%20World&category=class%20Category%20%7B%0A%20%20%20%20id%3A%20987%0A%20%20%20%20name%3A%20new%20category%0A%7D&photoUrls=http%3A%2F%2Fa.com&photoUrls=http%3A%2F%2Fb.com", p.path);
|
||||
String response = api.testQueryStyleFormExplodeTrueObject(queryObject);
|
||||
org.openapitools.client.EchoServerResponseParser p = new org.openapitools.client.EchoServerResponseParser(response);
|
||||
Assert.assertEquals("/query/style_form/explode_true/object?id=12345&name=Hello%20World&category=class%20Category%20%7B%0A%20%20%20%20id%3A%20987%0A%20%20%20%20name%3A%20new%20category%0A%7D&photoUrls=http%3A%2F%2Fa.com&photoUrls=http%3A%2F%2Fb.com", p.path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test query parameter(s)
|
||||
* <p>
|
||||
* Test query parameter(s)
|
||||
*
|
||||
* @throws ApiException if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
public void testQueryStyleDeepObjectExplodeTrueObject() 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"));
|
||||
|
||||
Assert.assertEquals("query_object[id]=12345&query_object[name]=Hello%20World&query_object[category][id]=987&query_object[category][name]=new%20category&query_object[photoUrls][0]=http%3A%2F%2Fa.com&query_object[photoUrls][1]=http%3A%2F%2Fb.com", queryObject.toUrlQueryString("query_object"));
|
||||
|
||||
String response = api.testQueryStyleDeepObjectExplodeTrueObject(queryObject);
|
||||
org.openapitools.client.EchoServerResponseParser p = new org.openapitools.client.EchoServerResponseParser(response);
|
||||
Assert.assertEquals("/query/style_deepObject/explode_true/object?query_object[id]=12345&query_object[name]=Hello%20World&query_object[category][id]=987&query_object[category][name]=new%20category&query_object[photoUrls][0]=http%3A%2F%2Fa.com&query_object[photoUrls][1]=http%3A%2F%2Fb.com", p.path);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user