Skip to content

feat(cts): add requestOptions tests #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ export function createTransporter({
cacheable: baseRequestOptions?.cacheable,
timeout: baseRequestOptions?.timeout,
queryParameters: {
...baseRequestOptions?.queryParameters,
...methodOptions.queryParameters,
...baseRequestOptions?.queryParameters,
},
headers: {
Accept: 'application/json',
...baseRequestOptions?.headers,
...methodOptions.headers,
...baseRequestOptions?.headers,
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type {
Headers,
Host,
QueryParameters,
Request,
RequestOptions,
QueryParameters,
Response,
StackFrame,
} from '../types';
Expand Down
19 changes: 9 additions & 10 deletions clients/algoliasearch-client-php/lib/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,12 @@ public static function deserialize($data, $class, $httpHeaders = null)
// If a discriminator is defined and points to a valid subclass, use it.
$discriminator = $class::DISCRIMINATOR;
if (
!empty($discriminator) &&
isset($data->{$discriminator}) &&
is_string($data->{$discriminator})
) {
!empty($discriminator) &&
isset($data->{$discriminator}) &&
is_string($data->{$discriminator})
) {
$subclass =
'\Algolia\AlgoliaSearch\Model\\' . $data->{$discriminator};
'\Algolia\AlgoliaSearch\Model\\' . $data->{$discriminator};
if (is_subclass_of($subclass, $class)) {
$class = $subclass;
}
Expand All @@ -419,15 +419,14 @@ public static function deserialize($data, $class, $httpHeaders = null)
$propertySetter = $instance::setters()[$property];

if (
!isset($propertySetter) ||
!isset($data->{$instance::attributeMap()[$property]})
) {
!isset($propertySetter) ||
!isset($data->{$instance::attributeMap()[$property]})
) {
continue;
}

if (isset($data->{$instance::attributeMap()[$property]})) {
$propertyValue =
$data->{$instance::attributeMap()[$property]};
$propertyValue = $data->{$instance::attributeMap()[$property]};
$instance->$propertySetter(
self::deserialize($propertyValue, $type, null)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,19 @@ private function normalize($options)
];

foreach ($options as $optionName => $value) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@damcou @millotp

After adding boolean tests, I realized PHP formatted booleans array like [true, true, false] to [1,1,], which would have been wrong in our case

I also realized that we were handling query parameters twice (here, and in buildQuery), so I've moved all the logic there

Could you please confirm it's correct to you? Tests pass correctly but better safe than sorry

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine ... But I'm pretty sure at some point I had the issue with the attributesToRetrieve param not being formatted correctly in the CTS so now I'm quite lost why it's working :D .

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's now done directly here in the buildQuery method, looking at the CTS it's still formatted correctly. Do you have any other stuff in mind we should check?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is the only issue of that type, I think we're good.

if (is_array($value)) {
if ($optionName === 'headers') {
$headersToLowerCase = [];
if (is_array($value) && $optionName === 'headers') {
$headersToLowerCase = [];

foreach ($value as $key => $v) {
$headersToLowerCase[mb_strtolower($key)] = $v;
}

$normalized[$optionName] = $this->format(
$headersToLowerCase
);
} else {
$normalized[$optionName] = $this->format(
$value,
$optionName === 'queryParameters'
);
foreach ($value as $key => $v) {
$headersToLowerCase[mb_strtolower($key)] = $v;
}

$normalized[$optionName] = $headersToLowerCase;
} else {
$normalized[$optionName] = $value;
}
}

return $normalized;
}

private function format($options, $isQueryParameters = false)
{
foreach ($options as $name => $value) {
if (is_array($value) && $isQueryParameters) {
$options[$name] = implode(',', $value);
}
}

return $options;
}
}
13 changes: 12 additions & 1 deletion clients/algoliasearch-client-php/lib/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ public static function buildQuery(array $args)

$args = array_map(function ($value) {
if (is_array($value)) {
return json_encode($value);
// PHP converts `true,false` in arrays to `1,`, so we create strings instead
// to avoid sending wrong values
$values = array_map(function ($v) {
if (is_bool($v)) {
return $v ? 'true' : 'false';
}

return $v;
}, $value);

// We then return the array as a string comma separated
return implode(',', $values);
Comment on lines 23 to +36
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what was duplicated in the RequestOptionFactory that I commented above

} elseif (is_bool($value)) {
return $value ? 'true' : 'false';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public Map<String, Object> postProcessSupportingFileData(
if (e.isSkipable()) {
System.exit(0);
}
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public CTSException(String message) {
super(message);
}

public CTSException(String message, Throwable cause) {
super(message, cause);
}

public CTSException(String message, boolean skipable) {
this(message);
this.skipable = skipable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,50 @@ public Map<String, Object> buildJSONForRequest(
test.put("testName", req.testName == null ? operationId : req.testName);
test.put("testIndex", testIndex);
test.put("request", req.request);

test.put("hasParameters", req.parameters.size() != 0);

if (req.requestOptions != null) {
test.put("hasRequestOptions", true);
test.put(
"requestOptions",
Json.mapper().writeValueAsString(req.requestOptions)
);
Map<String, Object> requestOptions = new HashMap<>();

if (req.requestOptions.queryParameters != null) {
CodegenParameter objSpec = new CodegenParameter();
objSpec.dataType =
inferDataType(req.requestOptions.queryParameters, objSpec, null);
requestOptions.put(
"queryParameters",
traverseParams(
"queryParameters",
req.requestOptions.queryParameters,
objSpec,
"",
0
)
);
}

if (req.requestOptions.headers != null) {
List<Map<String, String>> headers = new ArrayList<Map<String, String>>();

for (Entry<String, String> entry : req.requestOptions.headers.entrySet()) {
Map<String, String> parameter = new HashMap<>();

parameter.put("key", entry.getKey());
parameter.put("value", entry.getValue());

headers.add(parameter);
}

requestOptions.put("headers", headers);
}

test.put("requestOptionsWithDataType", requestOptions);
}

if (req.parameters.size() == 0) {
return test;
}
Expand Down Expand Up @@ -479,6 +520,21 @@ private String inferDataType(
if (spec != null) spec.setIsBoolean(true);
if (output != null) output.put("isBoolean", true);
return "Boolean";
case "ArrayList":
if (spec != null) {
spec.setIsArray(true);
// This is just to find the correct path in `handlePrimitive`,
// but it's not always the real type
CodegenProperty baseItems = new CodegenProperty();
baseItems.dataType = "String";
spec.setItems(baseItems);
}
if (output != null) output.put("isArray", true);
return "List";
case "LinkedHashMap":
if (spec != null) spec.baseType = "Object";
if (output != null) output.put("isFreeFormObject", true);
return "Object";
default:
throw new CTSException(
"Unknown type: " + param.getClass().getSimpleName()
Expand Down
18 changes: 18 additions & 0 deletions generators/src/main/java/com/algolia/codegen/cts/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Request {
public String testName;

public Map<String, Object> parameters;
public RequestOptions requestOptions;
public RequestProp request;

@Override
Expand All @@ -22,12 +23,29 @@ public String toString() {
sb.append("class Request {\n");
sb.append(" testName: ").append(testName).append("\n");
sb.append(" parameters: ").append(parameters).append("\n");
sb.append(" requestOptions: ").append(requestOptions).append("\n");
sb.append(" request: ").append(request).append("\n");
sb.append("}");
return sb.toString();
}
}

class RequestOptions {

public Map<String, Object> queryParameters;
public Map<String, String> headers;

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class RequestOptions {\n");
sb.append(" queryParameters: ").append(queryParameters).append("\n");
sb.append(" headers: ").append(headers).append("\n");
sb.append("}");
return sb.toString();
}
}

class RequestProp {

public String path;
Expand Down
Loading