Skip to content

Commit

Permalink
fix(java): use Map for query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed May 9, 2022
1 parent f8cee69 commit a0b6c74
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 266 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.algolia.utils.echo;

import com.algolia.Pair;
import java.util.List;
import java.util.Map;

public interface EchoResponseInterface {
public String getPath();
Expand All @@ -10,5 +9,5 @@ public interface EchoResponseInterface {

public String getBody();

public List<Pair> getQueryParams();
public Map<String, String> getQueryParams();
}
8 changes: 4 additions & 4 deletions scripts/ci/githubActions/createMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ async function getSpecMatrix(baseBranch: string): Promise<void> {
branch: baseBranch,
path,
});
const baseChanged = await isBaseChanged(
baseBranch,
MATRIX_DEPENDENCIES.common
);
const baseChanged = await isBaseChanged(baseBranch, {
...MATRIX_DEPENDENCIES.common,
...MATRIX_DEPENDENCIES.clients.common,
});

// No changes found, we don't put this job in the matrix
if (specChanges === 0 && !baseChanged) {
Expand Down
9 changes: 5 additions & 4 deletions templates/java/EchoResponse.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.HttpUrl;
import okhttp3.Request;
Expand All @@ -24,12 +25,12 @@ public class EchoResponse{{#apiInfo}}{{#apis}}{{{baseName}}}{{/apis}}{{/apiInfo}
}
}

private static List<Pair> buildQueryParams(Request req) {
List<Pair> params = new ArrayList<Pair>();
private static Map<String, String> buildQueryParams(Request req) {
Map<String, String> params = new HashMap<String, String>();
HttpUrl url = req.url();
for (String name : url.queryParameterNames()) {
for (String value : url.queryParameterValues(name)) {
params.add(new Pair(name, value));
params.put(name, value);
}
}
return params;
Expand All @@ -56,7 +57,7 @@ public class EchoResponse{{#apiInfo}}{{#apis}}{{{baseName}}}{{/apis}}{{/apiInfo}
return parseRequestBody(request);
}

public List<Pair> getQueryParams() {
public Map<String, String> getQueryParams() {
return buildQueryParams(request);
}

Expand Down
16 changes: 7 additions & 9 deletions templates/java/libraries/okhttp-gson/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public class ApiClient {
* @return The HTTP call
* @throws AlgoliaRuntimeException If fail to serialize the request body object
*/
public Call buildCall(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException {
public Call buildCall(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException {
Request request = buildRequest(path, method, queryParams, body, headerParams, callback);
return requester.newCall(request);
Expand All @@ -552,19 +552,17 @@ public class ApiClient {
* @return The HTTP request
* @throws AlgoliaRuntimeException If fail to serialize the request body object
*/
public Request buildRequest(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException {
public Request buildRequest(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException {
headerParams.put("X-Algolia-Application-Id", this.appId);
headerParams.put("X-Algolia-API-Key", this.apiKey);
headerParams.put("Accept", "application/json");
headerParams.put("Content-Type", "application/json");
final String url = buildUrl(path, queryParams);
final Request.Builder reqBuilder = new Request.Builder().url(url);
processHeaderParams(headerParams, reqBuilder);
String contentType = (String) headerParams.get("Content-Type");
// ensuring a default content type
if (contentType == null) {
contentType = "application/json";
}
RequestBody reqBody;
if (!HttpMethod.permitsRequestBody(method)) {
Expand Down Expand Up @@ -604,7 +602,7 @@ public class ApiClient {
* @param queryParams The query parameters
* @return The full URL
*/
public String buildUrl(String path, List<Pair> queryParams) {
public String buildUrl(String path, Map<String, String> queryParams) {
final StringBuilder url = new StringBuilder();
//The real host will be assigned by the retry strategy
Expand All @@ -613,7 +611,7 @@ public class ApiClient {
if (queryParams != null && !queryParams.isEmpty()) {
// support (constant) query string in `path`, e.g. "/posts?draft=1"
String prefix = path.contains("?") ? "&" : "?";
for (Pair param : queryParams) {
for (Entry<String, String> param : queryParams.entrySet()) {
if (param.getValue() != null) {
if (prefix != null) {
url.append(prefix);
Expand All @@ -622,7 +620,7 @@ public class ApiClient {
url.append("&");
}
String value = parameterToString(param.getValue());
url.append(escapeString(param.getName())).append("=").append(escapeString(value));
url.append(escapeString(param.getKey())).append("=").append(escapeString(value));
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions templates/java/libraries/okhttp-gson/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,17 @@ public class {{classname}} extends ApiClient {
{{#x-is-custom-request}}{{{paramName}}}.toString(){{/x-is-custom-request}}{{^x-is-custom-request}}this.escapeString({{{paramName}}}.toString()){{/x-is-custom-request}}
){{/pathParams}}{{/vendorExtensions}};

{{javaUtilPrefix}}List<Pair> queryParams = new {{javaUtilPrefix}}ArrayList<Pair>();
{{javaUtilPrefix}}Map<String, String> queryParams = new {{javaUtilPrefix}}HashMap<String, String>();
{{javaUtilPrefix}}Map<String, String> headers = new {{javaUtilPrefix}}HashMap<String, String>();

{{#vendorExtensions}}{{#queryParams}}
if ({{paramName}} != null) {
{{^x-is-custom-request}}
queryParams.addAll(this.parameterToPair("{{baseName}}", {{paramName}}));
queryParams.put("{{baseName}}", parameterToString({{paramName}}));
{{/x-is-custom-request}}
{{#x-is-custom-request}}
for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
queryParams.addAll(this.parameterToPair(parameter.getKey(), parameter.getValue()));
queryParams.put(parameter.getKey().toString(), parameterToString(parameter.getValue()));
}
{{/x-is-custom-request}}
}
Expand All @@ -142,8 +142,6 @@ public class {{classname}} extends ApiClient {
}

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

return this.buildCall(requestPath, "{{httpMethod}}", queryParams, bodyObj, headers, callback);
}
Expand Down
1 change: 1 addition & 0 deletions templates/javascript/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasR
{{/queryParams.0}}
{{/bodyParams.0}}
{{/allParams.0}}
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
*/
{{nickname}}(
{{#allParams.0}}
Expand Down
6 changes: 3 additions & 3 deletions tests/CTS/methods/requests/templates/java/requests.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class {{client}}Tests {

{{#request.queryParameters}}
Map<String, String> expectedQuery = JSON.deserialize("{{#lambda.escapequotes}}{{{request.queryParameters}}}{{/lambda.escapequotes}}", new TypeToken<HashMap<String, String>>() {}.getType());
List<Pair> actualQuery = req.getQueryParams();
Map<String, String> actualQuery = req.getQueryParams();
assertEquals(expectedQuery.size(), actualQuery.size());
for (Pair p : actualQuery) {
assertEquals(expectedQuery.get(p.getName()), p.getValue());
for (Map.Entry<String, String> p : actualQuery.entrySet()) {
assertEquals(expectedQuery.get(p.getKey()), p.getValue());
}
{{/request.queryParameters}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.algolia.JSON;
import com.algolia.Pair;
import com.algolia.api.AbtestingClient;
import com.algolia.model.abtesting.*;
import com.algolia.utils.echo.*;
Expand Down Expand Up @@ -111,10 +110,10 @@ void delTest1() {
"{\"query\":\"parameters\"}",
new TypeToken<HashMap<String, String>>() {}.getType()
);
List<Pair> actualQuery = req.getQueryParams();
Map<String, String> actualQuery = req.getQueryParams();
assertEquals(expectedQuery.size(), actualQuery.size());
for (Pair p : actualQuery) {
assertEquals(expectedQuery.get(p.getName()), p.getValue());
for (Map.Entry<String, String> p : actualQuery.entrySet()) {
assertEquals(expectedQuery.get(p.getKey()), p.getValue());
}
}

Expand Down Expand Up @@ -168,10 +167,10 @@ void getTest1() {
"{\"query\":\"parameters\"}",
new TypeToken<HashMap<String, String>>() {}.getType()
);
List<Pair> actualQuery = req.getQueryParams();
Map<String, String> actualQuery = req.getQueryParams();
assertEquals(expectedQuery.size(), actualQuery.size());
for (Pair p : actualQuery) {
assertEquals(expectedQuery.get(p.getName()), p.getValue());
for (Map.Entry<String, String> p : actualQuery.entrySet()) {
assertEquals(expectedQuery.get(p.getKey()), p.getValue());
}
}

Expand Down Expand Up @@ -207,10 +206,10 @@ void listABTestsTest0() {
"{\"offset\":\"42\",\"limit\":\"21\"}",
new TypeToken<HashMap<String, String>>() {}.getType()
);
List<Pair> actualQuery = req.getQueryParams();
Map<String, String> actualQuery = req.getQueryParams();
assertEquals(expectedQuery.size(), actualQuery.size());
for (Pair p : actualQuery) {
assertEquals(expectedQuery.get(p.getName()), p.getValue());
for (Map.Entry<String, String> p : actualQuery.entrySet()) {
assertEquals(expectedQuery.get(p.getKey()), p.getValue());
}
}

Expand Down Expand Up @@ -263,10 +262,10 @@ void postTest1() {
"{\"query\":\"parameters\"}",
new TypeToken<HashMap<String, String>>() {}.getType()
);
List<Pair> actualQuery = req.getQueryParams();
Map<String, String> actualQuery = req.getQueryParams();
assertEquals(expectedQuery.size(), actualQuery.size());
for (Pair p : actualQuery) {
assertEquals(expectedQuery.get(p.getName()), p.getValue());
for (Map.Entry<String, String> p : actualQuery.entrySet()) {
assertEquals(expectedQuery.get(p.getKey()), p.getValue());
}
}

Expand Down Expand Up @@ -319,10 +318,10 @@ void putTest1() {
"{\"query\":\"parameters\"}",
new TypeToken<HashMap<String, String>>() {}.getType()
);
List<Pair> actualQuery = req.getQueryParams();
Map<String, String> actualQuery = req.getQueryParams();
assertEquals(expectedQuery.size(), actualQuery.size());
for (Pair p : actualQuery) {
assertEquals(expectedQuery.get(p.getName()), p.getValue());
for (Map.Entry<String, String> p : actualQuery.entrySet()) {
assertEquals(expectedQuery.get(p.getKey()), p.getValue());
}
}

Expand Down
Loading

0 comments on commit a0b6c74

Please sign in to comment.