Skip to content

Commit 1630fad

Browse files
committed
fix !GET method body
1 parent 9ad169c commit 1630fad

File tree

6 files changed

+12
-13
lines changed

6 files changed

+12
-13
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ public RequestBody serialize(Object obj) throws AlgoliaRuntimeException {
244244
throw new AlgoliaRuntimeException(e);
245245
}
246246
} else {
247-
content = null;
247+
// An empty body is defaulted to "{}" to match an empty JSON object
248+
content = "{}";
248249
}
249250

250251
return RequestBody.create(content, MediaType.parse(this.contentType));
@@ -345,14 +346,6 @@ public Request buildRequest(
345346
RequestBody reqBody;
346347
if (!HttpMethod.permitsRequestBody(method)) {
347348
reqBody = null;
348-
} else if (body == null) {
349-
if ("DELETE".equals(method)) {
350-
// allow calling DELETE without sending a request body
351-
reqBody = null;
352-
} else {
353-
// use an empty request body (for POST, PUT and PATCH)
354-
reqBody = RequestBody.create("", MediaType.parse(this.contentType));
355-
}
356349
} else {
357350
reqBody = serialize(body);
358351
}

clients/algoliasearch-client-php/lib/RetryStrategy/ApiWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ private function createRequest(
280280
// Send an empty body instead of "[]" in case there are
281281
// no content/params to send
282282
if (empty($body)) {
283-
$body = '';
283+
$body = '{}';
284284
} else {
285285
$body = \json_encode($body, $this->jsonOptions);
286286
if (JSON_ERROR_NONE !== json_last_error()) {

generators/src/main/java/com/algolia/codegen/cts/tests/TestsRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
6060
test.put("method", operationId);
6161
test.put("testName", req.testName == null ? operationId : req.testName);
6262
test.put("testIndex", i);
63+
64+
// We've defaulted the body to be an empty object for all methods
65+
// but `GET` methods does not have a body.
66+
if (req.request.method.equals("GET")) {
67+
req.request.body = null;
68+
}
69+
6370
test.put("request", req.request);
6471
test.put("hasParameters", req.parameters.size() != 0);
6572

templates/java/tests/requests/requests.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class {{client}}RequestsTests {
6666

6767
assertEquals(req.path, "{{{request.path}}}");
6868
assertEquals(req.method, "{{{request.method}}}");
69-
7069
{{#request.body}}
7170
assertDoesNotThrow(() -> {
7271
JSONAssert.assertEquals("{{#lambda.escapeQuotes}}{{{request.body}}}{{/lambda.escapeQuotes}}", req.body, JSONCompareMode.STRICT);

templates/javascript/tests/requests/requests.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('{{operationId}}', () => {
2121

2222
expect(req.path).toEqual('{{{request.path}}}');
2323
expect(req.method).toEqual('{{{request.method}}}');
24-
expect(req.data).toEqual({{{request.body}}});
24+
expect(req.data).toEqual({{#request.body}}{{{.}}}{{/request.body}}{{^request.body}}undefined{{/request.body}});
2525
expect(req.searchParams).toStrictEqual({{#request.queryParameters}}{{{.}}}{{/request.queryParameters}}{{^request.queryParameters}}undefined{{/request.queryParameters}});
2626
{{#request.headers}}
2727
expect(req.headers).toEqual(

templates/php/tests/requests/requests.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class {{clientPrefix}}Test extends TestCase implements HttpClientInterface
115115
"path" => "{{{request.path}}}",
116116
"method" => "{{{request.method}}}",
117117
{{#request.body}}
118-
"body" => json_decode("{{#lambda.escapeQuotes}}{{{request.body}}}{{/lambda.escapeQuotes}}"),
118+
"body" => json_decode("{{#lambda.escapeQuotes}}{{{.}}}{{/lambda.escapeQuotes}}"),
119119
{{/request.body}}
120120
{{#request.queryParameters}}
121121
"queryParameters" => json_decode("{{#lambda.escapeQuotes}}{{{request.queryParameters}}}{{/lambda.escapeQuotes}}", true),

0 commit comments

Comments
 (0)