Skip to content

Commit 9320a5f

Browse files
authored
fix(templates): ensure requestOptions follow same format (#506)
1 parent 9486b78 commit 9320a5f

File tree

11 files changed

+56
-62
lines changed

11 files changed

+56
-62
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class RequestOptions {
1212

1313
private final Map<String, String> headers = new HashMap<String, String>();
14-
private final Map<String, String> queryParameters = new HashMap<String, String>();
14+
private final Map<String, Object> queryParameters = new HashMap<String, Object>();
1515
private Integer timeout = null;
1616

1717
public RequestOptions addExtraHeader(
@@ -24,7 +24,7 @@ public RequestOptions addExtraHeader(
2424

2525
public RequestOptions addExtraQueryParameters(
2626
@Nonnull String key,
27-
@Nonnull String value
27+
@Nonnull Object value
2828
) {
2929
queryParameters.put(key, value);
3030
return this;
@@ -34,7 +34,7 @@ public Map<String, String> getExtraHeaders() {
3434
return headers;
3535
}
3636

37-
public Map<String, String> getExtraQueryParameters() {
37+
public Map<String, Object> getExtraQueryParameters() {
3838
return queryParameters;
3939
}
4040

clients/algoliasearch-client-javascript/packages/client-common/src/transporter/createTransporter.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,30 @@ export function createTransporter({
106106
}
107107
: {};
108108

109-
const queryParameters = {
109+
const queryParameters: QueryParameters = {
110110
'x-algolia-agent': userAgent.value,
111111
...baseQueryParameters,
112112
...dataQueryParameters,
113-
...requestOptions.queryParameters,
114113
};
115114

115+
if (requestOptions?.queryParameters) {
116+
for (const [key, value] of Object.entries(
117+
requestOptions.queryParameters
118+
)) {
119+
// We want to keep `undefined` and `null` values,
120+
// but also avoid stringifying `object`s, as they are
121+
// handled in the `serializeUrl` step right after.
122+
if (
123+
!value ||
124+
Object.prototype.toString.call(value) === '[object Object]'
125+
) {
126+
queryParameters[key] = value;
127+
} else {
128+
queryParameters[key] = value.toString();
129+
}
130+
}
131+
}
132+
116133
let timeoutsCount = 0;
117134

118135
const retry = async (

clients/algoliasearch-client-javascript/packages/client-common/src/transporter/helpers.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
Host,
44
Request,
55
RequestOptions,
6+
QueryParameters,
67
Response,
78
StackFrame,
89
} from '../types';
@@ -40,9 +41,7 @@ export function serializeUrl(
4041
return url;
4142
}
4243

43-
export function serializeQueryParameters(
44-
parameters: Readonly<Record<string, any>>
45-
): string {
44+
export function serializeQueryParameters(parameters: QueryParameters): string {
4645
const isObjectOrArray = (value: any): boolean =>
4746
Object.prototype.toString.call(value) === '[object Object]' ||
4847
Object.prototype.toString.call(value) === '[object Array]';

clients/algoliasearch-client-javascript/packages/client-common/src/types/Transporter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import type { Host } from './Host';
33
import type { Request, Requester, EndRequest, Response } from './Requester';
44

55
export type Headers = Record<string, string>;
6-
7-
export type QueryParameters = Record<string, string>;
6+
export type QueryParameters = Record<string, any>;
87

98
export type RequestOptions = {
109
/**

clients/algoliasearch-client-php/lib/RequestOptions/RequestOptionsFactory.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ private function normalize($options)
7575
$headersToLowerCase
7676
);
7777
} else {
78-
$normalized[$optionName] = $this->format($value);
78+
$normalized[$optionName] = $this->format(
79+
$value,
80+
$optionName === 'queryParameters'
81+
);
7982
}
8083
} else {
8184
$normalized[$optionName] = $value;
@@ -85,21 +88,14 @@ private function normalize($options)
8588
return $normalized;
8689
}
8790

88-
private function format($options)
91+
private function format($options, $isQueryParameters = false)
8992
{
9093
foreach ($options as $name => $value) {
91-
if (in_array($name, self::getAttributesToFormat(), true)) {
92-
if (is_array($value)) {
93-
$options[$name] = implode(',', $value);
94-
}
94+
if (is_array($value) && $isQueryParameters) {
95+
$options[$name] = implode(',', $value);
9596
}
9697
}
9798

9899
return $options;
99100
}
100-
101-
public static function getAttributesToFormat()
102-
{
103-
return ['attributesToRetrieve', 'type'];
104-
}
105101
}

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public class ApiClient {
273273
* @return The HTTP call
274274
* @throws AlgoliaRuntimeException If fail to serialize the request body object
275275
*/
276-
public Call buildCall(String path, String method, Map<String, String> queryParameters, Object body, Map<String, String> headerParams, RequestOptions requestOptions) throws AlgoliaRuntimeException {
276+
public Call buildCall(String path, String method, Map<String, Object> queryParameters, Object body, Map<String, String> headerParams, RequestOptions requestOptions) throws AlgoliaRuntimeException {
277277
Request request = buildRequest(path, method, queryParameters, body, headerParams, requestOptions);
278278
279279
return requester.newCall(request);
@@ -291,7 +291,7 @@ public class ApiClient {
291291
* @return The HTTP request
292292
* @throws AlgoliaRuntimeException If fail to serialize the request body object
293293
*/
294-
public Request buildRequest(String path, String method, Map<String, String> queryParameters, Object body, Map<String, String> headerParams, RequestOptions requestOptions) throws AlgoliaRuntimeException {
294+
public Request buildRequest(String path, String method, Map<String, Object> queryParameters, Object body, Map<String, String> headerParams, RequestOptions requestOptions) throws AlgoliaRuntimeException {
295295
boolean hasRequestOptions = requestOptions != null;
296296
final String url = buildUrl(
297297
path,
@@ -331,35 +331,26 @@ public class ApiClient {
331331
* @param extraQueryParameters The query parameters, coming from the requestOptions
332332
* @return The full URL
333333
*/
334-
public String buildUrl(String path, Map<String, String> queryParameters, Map<String, String> extraQueryParameters) {
335-
StringBuilder url = new StringBuilder();
336-
337-
//The real host will be assigned by the retry strategy
338-
url.append("http://temp.path").append(path);
334+
public String buildUrl(
335+
String path,
336+
Map<String, Object> queryParameters,
337+
Map<String, Object> extraQueryParameters
338+
) {
339+
if (extraQueryParameters != null) {
340+
for (Entry<String, Object> param : extraQueryParameters.entrySet()) {
341+
queryParameters.put(param.getKey(), param.getValue());
342+
}
343+
}
339344
340-
url = parseQueryParameters(path, url, queryParameters);
341-
url = parseQueryParameters(path, url, extraQueryParameters);
345+
final StringBuilder url = new StringBuilder();
342346
343-
return url.toString();
344-
}
347+
// The real host will be assigned by the retry strategy
348+
url.append("http://temp.path").append(path);
345349
346-
/**
347-
* Parses the given map of Query Parameters to a given URL.
348-
*
349-
* @param path The sub path
350-
* @param url The url to add queryParameters to
351-
* @param queryParameters The query parameters
352-
* @return The URL
353-
*/
354-
public StringBuilder parseQueryParameters(
355-
String path,
356-
StringBuilder url,
357-
Map<String, String> queryParameters
358-
) {
359350
if (queryParameters != null && !queryParameters.isEmpty()) {
360351
// support (constant) query string in `path`, e.g. "/posts?draft=1"
361352
String prefix = path.contains("?") ? "&" : "?";
362-
for (Entry<String, String> param : queryParameters.entrySet()) {
353+
for (Entry<String, Object> param : queryParameters.entrySet()) {
363354
if (param.getValue() != null) {
364355
if (prefix != null) {
365356
url.append(prefix);
@@ -376,7 +367,7 @@ public class ApiClient {
376367
}
377368
}
378369
379-
return url;
370+
return url.toString();
380371
}
381372
382373
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public class {{classname}} extends ApiClient {
170170
{{#x-is-custom-request}}{{{paramName}}}.toString(){{/x-is-custom-request}}{{^x-is-custom-request}}this.escapeString({{{paramName}}}.toString()){{/x-is-custom-request}}
171171
){{/pathParams}}{{/vendorExtensions}};
172172

173-
{{javaUtilPrefix}}Map<String, String> queryParameters = new {{javaUtilPrefix}}HashMap<String, String>();
173+
{{javaUtilPrefix}}Map<String, Object> queryParameters = new {{javaUtilPrefix}}HashMap<String, Object>();
174174
{{javaUtilPrefix}}Map<String, String> headers = new {{javaUtilPrefix}}HashMap<String, String>();
175175

176176
{{#vendorExtensions}}{{#queryParams}}

templates/php/api.mustache

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use {{invokerPackage}}\Algolia;
66
use {{invokerPackage}}\ApiException;
77
use {{invokerPackage}}\Configuration\{{configClassname}};
88
use {{invokerPackage}}\ObjectSerializer;
9-
use {{invokerPackage}}\RequestOptions\RequestOptionsFactory;
109
use {{invokerPackage}}\RetryStrategy\ApiWrapper;
1110
use {{invokerPackage}}\RetryStrategy\ApiWrapperInterface;
1211
use {{invokerPackage}}\RetryStrategy\ClusterHosts;
@@ -213,14 +212,7 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
213212
{{#isExplode}}
214213
if (${{paramName}} !== null) {
215214
{{#style}}
216-
if(is_array(${{paramName}}) && ! in_array('{{paramName}}', RequestOptionsFactory::getAttributesToFormat())) {
217-
foreach(${{paramName}} as $key => $value) {
218-
$queryParameters[$key] = $value;
219-
}
220-
}
221-
else {
222-
$queryParameters{{^x-is-custom-request}}['{{baseName}}']{{/x-is-custom-request}} = ${{paramName}};
223-
}
215+
$queryParameters{{^x-is-custom-request}}['{{baseName}}']{{/x-is-custom-request}} = ${{paramName}};
224216
{{/style}}
225217
{{^style}}
226218
$queryParameters['{{baseName}}'] = ${{paramName}};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ class {{client}}Tests {
5555

5656
{{#request.queryParameters}}
5757
Map<String, String> expectedQuery = JSON.deserialize("{{#lambda.escapequotes}}{{{request.queryParameters}}}{{/lambda.escapequotes}}", new TypeToken<HashMap<String, String>>() {}.getType());
58-
Map<String, String> actualQuery = req.queryParameters;
58+
Map<String, Object> actualQuery = req.queryParameters;
5959

6060
assertEquals(expectedQuery.size(), actualQuery.size());
61-
for (Map.Entry<String, String> p : actualQuery.entrySet()) {
61+
for (Map.Entry<String, Object> p : actualQuery.entrySet()) {
6262
assertEquals(expectedQuery.get(p.getKey()), p.getValue());
6363
}
6464
{{/request.queryParameters}}

tests/output/java/src/test/java/com/algolia/CallEcho.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ private String processResponseBody() {
5151
}
5252
}
5353

54-
private Map<String, String> buildQueryParameters() {
55-
Map<String, String> params = new HashMap<>();
54+
private Map<String, Object> buildQueryParameters() {
55+
Map<String, Object> params = new HashMap<>();
5656
HttpUrl url = request.url();
5757
for (String name : url.queryParameterNames()) {
5858
for (String value : url.queryParameterValues(name)) {

tests/output/java/src/test/java/com/algolia/EchoResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class EchoResponse {
77
public String path;
88
public String method;
99
public String body;
10-
public Map<String, String> queryParameters;
10+
public Map<String, Object> queryParameters;
1111
public Map<String, String> headers;
1212
}

0 commit comments

Comments
 (0)