Skip to content

Commit 949c3f2

Browse files
authored
fix(java): use Map for query parameters (#484)
1 parent 351e811 commit 949c3f2

File tree

18 files changed

+270
-480
lines changed

18 files changed

+270
-480
lines changed

clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/Pair.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.algolia.utils.echo;
22

3-
import com.algolia.Pair;
4-
import java.util.List;
3+
import java.util.Map;
54

65
public interface EchoResponseInterface {
76
public String getPath();
@@ -10,5 +9,5 @@ public interface EchoResponseInterface {
109

1110
public String getBody();
1211

13-
public List<Pair> getQueryParams();
12+
public Map<String, String> getQueryParams();
1413
}

generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ public void processOpts() {
152152
supportingFiles.removeIf(file ->
153153
file.getTemplateFile().equals("build.gradle.mustache") ||
154154
file.getTemplateFile().equals("settings.gradle.mustache") ||
155-
file.getTemplateFile().equals("gitignore.mustache")
155+
file.getTemplateFile().equals("gitignore.mustache") ||
156+
file.getTemplateFile().equals("Pair.mustache")
156157
);
157158
}
158159

scripts/ci/githubActions/createMatrix.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ async function getSpecMatrix(baseBranch: string): Promise<void> {
138138
branch: baseBranch,
139139
path,
140140
});
141-
const baseChanged = await isBaseChanged(
142-
baseBranch,
143-
MATRIX_DEPENDENCIES.common
144-
);
141+
const baseChanged = await isBaseChanged(baseBranch, {
142+
...MATRIX_DEPENDENCIES.common,
143+
...MATRIX_DEPENDENCIES.clients.common,
144+
});
145145

146146
// No changes found, we don't put this job in the matrix
147147
if (specChanges === 0 && !baseChanged) {

templates/java/EchoResponse.mustache

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.io.IOException;
66
import java.util.ArrayList;
77
import java.util.HashMap;
88
import java.util.List;
9+
import java.util.Map;
910

1011
import okhttp3.HttpUrl;
1112
import okhttp3.Request;
@@ -24,12 +25,12 @@ public class EchoResponse{{#apiInfo}}{{#apis}}{{{baseName}}}{{/apis}}{{/apiInfo}
2425
}
2526
}
2627

27-
private static List<Pair> buildQueryParams(Request req) {
28-
List<Pair> params = new ArrayList<Pair>();
28+
private static Map<String, String> buildQueryParams(Request req) {
29+
Map<String, String> params = new HashMap<String, String>();
2930
HttpUrl url = req.url();
3031
for (String name : url.queryParameterNames()) {
3132
for (String value : url.queryParameterValues(name)) {
32-
params.add(new Pair(name, value));
33+
params.put(name, value);
3334
}
3435
}
3536
return params;
@@ -56,7 +57,7 @@ public class EchoResponse{{#apiInfo}}{{#apis}}{{{baseName}}}{{/apis}}{{/apiInfo}
5657
return parseRequestBody(request);
5758
}
5859

59-
public List<Pair> getQueryParams() {
60+
public Map<String, String> getQueryParams() {
6061
return buildQueryParams(request);
6162
}
6263

templates/java/Pair.mustache

Lines changed: 0 additions & 47 deletions
This file was deleted.

templates/java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 7 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -190,111 +190,6 @@ public class ApiClient {
190190
}
191191
}
192192
193-
/**
194-
* Formats the specified query parameter to a list containing a single {@code Pair} object.
195-
*
196-
* Note that {@code value} must not be a collection.
197-
*
198-
* @param name The name of the parameter.
199-
* @param value The value of the parameter.
200-
* @return A list containing a single {@code Pair} object.
201-
*/
202-
public List<Pair> parameterToPair(String name, Object value) {
203-
List<Pair> params = new ArrayList<Pair>();
204-
205-
// preconditions
206-
if (name == null || name.isEmpty() || value == null) {
207-
return params;
208-
}
209-
210-
params.add(new Pair(name, parameterToString(value)));
211-
return params;
212-
}
213-
214-
/**
215-
* Formats the specified collection query parameters to a list of {@code Pair} objects.
216-
*
217-
* Note that the values of each of the returned Pair objects are percent-encoded.
218-
*
219-
* @param collectionFormat The collection format of the parameter.
220-
* @param name The name of the parameter.
221-
* @param value The value of the parameter.
222-
* @return A list of {@code Pair} objects.
223-
*/
224-
public List<Pair> parameterToPairs(String collectionFormat, String name, Collection value) {
225-
List<Pair> params = new ArrayList<Pair>();
226-
227-
// preconditions
228-
if (name == null || name.isEmpty() || value == null || value.isEmpty()) {
229-
return params;
230-
}
231-
232-
// create the params based on the collection format
233-
if ("multi".equals(collectionFormat)) {
234-
for (Object item : value) {
235-
params.add(new Pair(name, escapeString(parameterToString(item))));
236-
}
237-
return params;
238-
}
239-
240-
// collectionFormat is assumed to be "csv" by default
241-
String delimiter = ",";
242-
243-
// escape all delimiters except commas, which are URI reserved
244-
// characters
245-
if ("ssv".equals(collectionFormat)) {
246-
delimiter = escapeString(" ");
247-
} else if ("tsv".equals(collectionFormat)) {
248-
delimiter = escapeString("\t");
249-
} else if ("pipes".equals(collectionFormat)) {
250-
delimiter = escapeString("|");
251-
}
252-
253-
StringBuilder sb = new StringBuilder();
254-
for (Object item : value) {
255-
sb.append(delimiter);
256-
sb.append(escapeString(parameterToString(item)));
257-
}
258-
259-
params.add(new Pair(name, sb.substring(delimiter.length())));
260-
261-
return params;
262-
}
263-
264-
/**
265-
* Formats the specified collection path parameter to a string value.
266-
*
267-
* @param collectionFormat The collection format of the parameter.
268-
* @param value The value of the parameter.
269-
* @return String representation of the parameter
270-
*/
271-
public String collectionPathParameterToString(String collectionFormat, Collection value) {
272-
// create the value based on the collection format
273-
if ("multi".equals(collectionFormat)) {
274-
// not valid for path params
275-
return parameterToString(value);
276-
}
277-
278-
// collectionFormat is assumed to be "csv" by default
279-
String delimiter = ",";
280-
281-
if ("ssv".equals(collectionFormat)) {
282-
delimiter = " ";
283-
} else if ("tsv".equals(collectionFormat)) {
284-
delimiter = "\t";
285-
} else if ("pipes".equals(collectionFormat)) {
286-
delimiter = "|";
287-
}
288-
289-
StringBuilder sb = new StringBuilder() ;
290-
for (Object item : value) {
291-
sb.append(delimiter);
292-
sb.append(parameterToString(item));
293-
}
294-
295-
return sb.substring(delimiter.length());
296-
}
297-
298193
/**
299194
* Check if the given MIME is a JSON MIME.
300195
* JSON MIME examples:
@@ -534,7 +429,7 @@ public class ApiClient {
534429
* @return The HTTP call
535430
* @throws AlgoliaRuntimeException If fail to serialize the request body object
536431
*/
537-
public Call buildCall(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException {
432+
public Call buildCall(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException {
538433
Request request = buildRequest(path, method, queryParams, body, headerParams, callback);
539434
540435
return requester.newCall(request);
@@ -552,19 +447,17 @@ public class ApiClient {
552447
* @return The HTTP request
553448
* @throws AlgoliaRuntimeException If fail to serialize the request body object
554449
*/
555-
public Request buildRequest(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException {
450+
public Request buildRequest(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException {
556451
headerParams.put("X-Algolia-Application-Id", this.appId);
557452
headerParams.put("X-Algolia-API-Key", this.apiKey);
453+
headerParams.put("Accept", "application/json");
454+
headerParams.put("Content-Type", "application/json");
558455
559456
final String url = buildUrl(path, queryParams);
560457
final Request.Builder reqBuilder = new Request.Builder().url(url);
561458
processHeaderParams(headerParams, reqBuilder);
562459
563460
String contentType = (String) headerParams.get("Content-Type");
564-
// ensuring a default content type
565-
if (contentType == null) {
566-
contentType = "application/json";
567-
}
568461
569462
RequestBody reqBody;
570463
if (!HttpMethod.permitsRequestBody(method)) {
@@ -604,7 +497,7 @@ public class ApiClient {
604497
* @param queryParams The query parameters
605498
* @return The full URL
606499
*/
607-
public String buildUrl(String path, List<Pair> queryParams) {
500+
public String buildUrl(String path, Map<String, String> queryParams) {
608501
final StringBuilder url = new StringBuilder();
609502
610503
//The real host will be assigned by the retry strategy
@@ -613,7 +506,7 @@ public class ApiClient {
613506
if (queryParams != null && !queryParams.isEmpty()) {
614507
// support (constant) query string in `path`, e.g. "/posts?draft=1"
615508
String prefix = path.contains("?") ? "&" : "?";
616-
for (Pair param : queryParams) {
509+
for (Entry<String, String> param : queryParams.entrySet()) {
617510
if (param.getValue() != null) {
618511
if (prefix != null) {
619512
url.append(prefix);
@@ -622,7 +515,7 @@ public class ApiClient {
622515
url.append("&");
623516
}
624517
String value = parameterToString(param.getValue());
625-
url.append(escapeString(param.getName())).append("=").append(escapeString(value));
518+
url.append(escapeString(param.getKey())).append("=").append(escapeString(value));
626519
}
627520
}
628521
}

templates/java/libraries/okhttp-gson/api.mustache

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,17 @@ public class {{classname}} extends ApiClient {
120120
{{#x-is-custom-request}}{{{paramName}}}.toString(){{/x-is-custom-request}}{{^x-is-custom-request}}this.escapeString({{{paramName}}}.toString()){{/x-is-custom-request}}
121121
){{/pathParams}}{{/vendorExtensions}};
122122

123-
{{javaUtilPrefix}}List<Pair> queryParams = new {{javaUtilPrefix}}ArrayList<Pair>();
123+
{{javaUtilPrefix}}Map<String, String> queryParams = new {{javaUtilPrefix}}HashMap<String, String>();
124124
{{javaUtilPrefix}}Map<String, String> headers = new {{javaUtilPrefix}}HashMap<String, String>();
125125

126126
{{#vendorExtensions}}{{#queryParams}}
127127
if ({{paramName}} != null) {
128128
{{^x-is-custom-request}}
129-
queryParams.addAll(this.parameterToPair("{{baseName}}", {{paramName}}));
129+
queryParams.put("{{baseName}}", parameterToString({{paramName}}));
130130
{{/x-is-custom-request}}
131131
{{#x-is-custom-request}}
132132
for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
133-
queryParams.addAll(this.parameterToPair(parameter.getKey(), parameter.getValue()));
133+
queryParams.put(parameter.getKey().toString(), parameterToString(parameter.getValue()));
134134
}
135135
{{/x-is-custom-request}}
136136
}
@@ -142,8 +142,6 @@ public class {{classname}} extends ApiClient {
142142
}
143143

144144
{{/headerParams}}
145-
headers.put("Accept", "application/json");
146-
headers.put("Content-Type", "application/json");
147145

148146
return this.buildCall(requestPath, "{{httpMethod}}", queryParams, bodyObj, headers, callback);
149147
}

templates/javascript/api-single.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasR
149149
{{/queryParams.0}}
150150
{{/bodyParams.0}}
151151
{{/allParams.0}}
152+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
152153
*/
153154
{{nickname}}(
154155
{{#allParams.0}}

tests/CTS/methods/requests/templates/java/requests.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ class {{client}}Tests {
5151

5252
{{#request.queryParameters}}
5353
Map<String, String> expectedQuery = JSON.deserialize("{{#lambda.escapequotes}}{{{request.queryParameters}}}{{/lambda.escapequotes}}", new TypeToken<HashMap<String, String>>() {}.getType());
54-
List<Pair> actualQuery = req.getQueryParams();
54+
Map<String, String> actualQuery = req.getQueryParams();
5555
assertEquals(expectedQuery.size(), actualQuery.size());
56-
for (Pair p : actualQuery) {
57-
assertEquals(expectedQuery.get(p.getName()), p.getValue());
56+
for (Map.Entry<String, String> p : actualQuery.entrySet()) {
57+
assertEquals(expectedQuery.get(p.getKey()), p.getValue());
5858
}
5959
{{/request.queryParameters}}
6060
}

0 commit comments

Comments
 (0)