Skip to content

Commit ba2689a

Browse files
authored
feat(cts): add requestOptions tests (#501)
1 parent 39b3025 commit ba2689a

File tree

15 files changed

+445
-43
lines changed

15 files changed

+445
-43
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,13 @@ export function createTransporter({
257257
cacheable: baseRequestOptions?.cacheable,
258258
timeout: baseRequestOptions?.timeout,
259259
queryParameters: {
260-
...baseRequestOptions?.queryParameters,
261260
...methodOptions.queryParameters,
261+
...baseRequestOptions?.queryParameters,
262262
},
263263
headers: {
264264
Accept: 'application/json',
265-
...baseRequestOptions?.headers,
266265
...methodOptions.headers,
266+
...baseRequestOptions?.headers,
267267
},
268268
};
269269

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type {
22
Headers,
33
Host,
4+
QueryParameters,
45
Request,
56
RequestOptions,
6-
QueryParameters,
77
Response,
88
StackFrame,
99
} from '../types';

clients/algoliasearch-client-php/lib/ObjectSerializer.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,12 @@ public static function deserialize($data, $class, $httpHeaders = null)
402402
// If a discriminator is defined and points to a valid subclass, use it.
403403
$discriminator = $class::DISCRIMINATOR;
404404
if (
405-
!empty($discriminator) &&
406-
isset($data->{$discriminator}) &&
407-
is_string($data->{$discriminator})
408-
) {
405+
!empty($discriminator) &&
406+
isset($data->{$discriminator}) &&
407+
is_string($data->{$discriminator})
408+
) {
409409
$subclass =
410-
'\Algolia\AlgoliaSearch\Model\\' . $data->{$discriminator};
410+
'\Algolia\AlgoliaSearch\Model\\' . $data->{$discriminator};
411411
if (is_subclass_of($subclass, $class)) {
412412
$class = $subclass;
413413
}
@@ -419,15 +419,14 @@ public static function deserialize($data, $class, $httpHeaders = null)
419419
$propertySetter = $instance::setters()[$property];
420420

421421
if (
422-
!isset($propertySetter) ||
423-
!isset($data->{$instance::attributeMap()[$property]})
424-
) {
422+
!isset($propertySetter) ||
423+
!isset($data->{$instance::attributeMap()[$property]})
424+
) {
425425
continue;
426426
}
427427

428428
if (isset($data->{$instance::attributeMap()[$property]})) {
429-
$propertyValue =
430-
$data->{$instance::attributeMap()[$property]};
429+
$propertyValue = $data->{$instance::attributeMap()[$property]};
431430
$instance->$propertySetter(
432431
self::deserialize($propertyValue, $type, null)
433432
);

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

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,39 +63,19 @@ private function normalize($options)
6363
];
6464

6565
foreach ($options as $optionName => $value) {
66-
if (is_array($value)) {
67-
if ($optionName === 'headers') {
68-
$headersToLowerCase = [];
66+
if (is_array($value) && $optionName === 'headers') {
67+
$headersToLowerCase = [];
6968

70-
foreach ($value as $key => $v) {
71-
$headersToLowerCase[mb_strtolower($key)] = $v;
72-
}
73-
74-
$normalized[$optionName] = $this->format(
75-
$headersToLowerCase
76-
);
77-
} else {
78-
$normalized[$optionName] = $this->format(
79-
$value,
80-
$optionName === 'queryParameters'
81-
);
69+
foreach ($value as $key => $v) {
70+
$headersToLowerCase[mb_strtolower($key)] = $v;
8271
}
72+
73+
$normalized[$optionName] = $headersToLowerCase;
8374
} else {
8475
$normalized[$optionName] = $value;
8576
}
8677
}
8778

8879
return $normalized;
8980
}
90-
91-
private function format($options, $isQueryParameters = false)
92-
{
93-
foreach ($options as $name => $value) {
94-
if (is_array($value) && $isQueryParameters) {
95-
$options[$name] = implode(',', $value);
96-
}
97-
}
98-
99-
return $options;
100-
}
10181
}

clients/algoliasearch-client-php/lib/Support/Helpers.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ public static function buildQuery(array $args)
2222

2323
$args = array_map(function ($value) {
2424
if (is_array($value)) {
25-
return json_encode($value);
25+
// PHP converts `true,false` in arrays to `1,`, so we create strings instead
26+
// to avoid sending wrong values
27+
$values = array_map(function ($v) {
28+
if (is_bool($v)) {
29+
return $v ? 'true' : 'false';
30+
}
31+
32+
return $v;
33+
}, $value);
34+
35+
// We then return the array as a string comma separated
36+
return implode(',', $values);
2637
} elseif (is_bool($value)) {
2738
return $value ? 'true' : 'false';
2839
}

generators/src/main/java/com/algolia/codegen/cts/AlgoliaCtsGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public Map<String, Object> postProcessSupportingFileData(
194194
if (e.isSkipable()) {
195195
System.exit(0);
196196
}
197+
e.printStackTrace();
197198
System.exit(1);
198199
} catch (Exception e) {
199200
e.printStackTrace();

generators/src/main/java/com/algolia/codegen/cts/CTSException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ public CTSException(String message) {
88
super(message);
99
}
1010

11+
public CTSException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
1115
public CTSException(String message, boolean skipable) {
1216
this(message);
1317
this.skipable = skipable;

generators/src/main/java/com/algolia/codegen/cts/ParametersWithDataType.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,50 @@ public Map<String, Object> buildJSONForRequest(
3939
test.put("testName", req.testName == null ? operationId : req.testName);
4040
test.put("testIndex", testIndex);
4141
test.put("request", req.request);
42-
4342
test.put("hasParameters", req.parameters.size() != 0);
4443

44+
if (req.requestOptions != null) {
45+
test.put("hasRequestOptions", true);
46+
test.put(
47+
"requestOptions",
48+
Json.mapper().writeValueAsString(req.requestOptions)
49+
);
50+
Map<String, Object> requestOptions = new HashMap<>();
51+
52+
if (req.requestOptions.queryParameters != null) {
53+
CodegenParameter objSpec = new CodegenParameter();
54+
objSpec.dataType =
55+
inferDataType(req.requestOptions.queryParameters, objSpec, null);
56+
requestOptions.put(
57+
"queryParameters",
58+
traverseParams(
59+
"queryParameters",
60+
req.requestOptions.queryParameters,
61+
objSpec,
62+
"",
63+
0
64+
)
65+
);
66+
}
67+
68+
if (req.requestOptions.headers != null) {
69+
List<Map<String, String>> headers = new ArrayList<Map<String, String>>();
70+
71+
for (Entry<String, String> entry : req.requestOptions.headers.entrySet()) {
72+
Map<String, String> parameter = new HashMap<>();
73+
74+
parameter.put("key", entry.getKey());
75+
parameter.put("value", entry.getValue());
76+
77+
headers.add(parameter);
78+
}
79+
80+
requestOptions.put("headers", headers);
81+
}
82+
83+
test.put("requestOptionsWithDataType", requestOptions);
84+
}
85+
4586
if (req.parameters.size() == 0) {
4687
return test;
4788
}
@@ -479,6 +520,21 @@ private String inferDataType(
479520
if (spec != null) spec.setIsBoolean(true);
480521
if (output != null) output.put("isBoolean", true);
481522
return "Boolean";
523+
case "ArrayList":
524+
if (spec != null) {
525+
spec.setIsArray(true);
526+
// This is just to find the correct path in `handlePrimitive`,
527+
// but it's not always the real type
528+
CodegenProperty baseItems = new CodegenProperty();
529+
baseItems.dataType = "String";
530+
spec.setItems(baseItems);
531+
}
532+
if (output != null) output.put("isArray", true);
533+
return "List";
534+
case "LinkedHashMap":
535+
if (spec != null) spec.baseType = "Object";
536+
if (output != null) output.put("isFreeFormObject", true);
537+
return "Object";
482538
default:
483539
throw new CTSException(
484540
"Unknown type: " + param.getClass().getSimpleName()

generators/src/main/java/com/algolia/codegen/cts/Request.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Request {
1414
public String testName;
1515

1616
public Map<String, Object> parameters;
17+
public RequestOptions requestOptions;
1718
public RequestProp request;
1819

1920
@Override
@@ -22,12 +23,29 @@ public String toString() {
2223
sb.append("class Request {\n");
2324
sb.append(" testName: ").append(testName).append("\n");
2425
sb.append(" parameters: ").append(parameters).append("\n");
26+
sb.append(" requestOptions: ").append(requestOptions).append("\n");
2527
sb.append(" request: ").append(request).append("\n");
2628
sb.append("}");
2729
return sb.toString();
2830
}
2931
}
3032

33+
class RequestOptions {
34+
35+
public Map<String, Object> queryParameters;
36+
public Map<String, String> headers;
37+
38+
@Override
39+
public String toString() {
40+
StringBuilder sb = new StringBuilder();
41+
sb.append("class RequestOptions {\n");
42+
sb.append(" queryParameters: ").append(queryParameters).append("\n");
43+
sb.append(" headers: ").append(headers).append("\n");
44+
sb.append("}");
45+
return sb.toString();
46+
}
47+
}
48+
3149
class RequestProp {
3250

3351
public String path;

0 commit comments

Comments
 (0)