Skip to content

Commit 9b758b5

Browse files
committed
fix(clients): send empty object on empty request body
1 parent 1d2669a commit 9b758b5

File tree

3 files changed

+5
-13
lines changed

3 files changed

+5
-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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,9 @@ private function createRequest(
277277
$protocolVersion = '1.1'
278278
) {
279279
if (is_array($body)) {
280-
// Send an empty body instead of "[]" in case there are
281-
// no content/params to send
280+
// An empty body is defaulted to "{}" to match an empty JSON object
282281
if (empty($body)) {
283-
$body = '';
282+
$body = '{}';
284283
} else {
285284
$body = \json_encode($body, $this->jsonOptions);
286285
if (JSON_ERROR_NONE !== json_last_error()) {

config/generation.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = {
2727
'!clients/algoliasearch-client-javascript/packages/algoliasearch/jest.config.ts',
2828

2929
// PHP
30-
'!clients/algoliasearch-client-php/*',
30+
'!clients/algoliasearch-client-php/**',
3131
'clients/algoliasearch-client-php/lib/Api/*',
3232
'clients/algoliasearch-client-php/lib/Model/**',
3333
'clients/algoliasearch-client-php/lib/Configuration/*',

0 commit comments

Comments
 (0)