Skip to content

Commit 2af447c

Browse files
committed
Prevent empty arrays in request body
1 parent 02c69e4 commit 2af447c

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Classes/Domain/Model/Index.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ public function request(string $method, string $path = null, array $arguments =
191191
*/
192192
public function create(): void
193193
{
194-
$indexConfiguration = $this->getConfiguration();
195-
$indexCreateObject = array_filter($indexConfiguration, static fn($key) => in_array($key, self::$allowedIndexCreateKeys, true), ARRAY_FILTER_USE_KEY);
196-
$this->request('PUT', null, [], json_encode($indexCreateObject));
194+
$indexConfiguration = $this->getConfiguration() ?? [];
195+
$indexCreateObject = is_array($indexConfiguration) ? array_filter($indexConfiguration, static fn($key) => in_array($key, self::$allowedIndexCreateKeys, true), ARRAY_FILTER_USE_KEY) : [];
196+
$this->request('PUT', null, [], $this->encodeRequestBody($indexCreateObject));
197197
}
198198

199199
/**
@@ -227,7 +227,7 @@ public function updateSettings(): void
227227
}
228228
}
229229
if ($updatableSettings !== []) {
230-
$this->request('PUT', '/_settings', [], json_encode($updatableSettings));
230+
$this->request('PUT', '/_settings', [], $this->encodeRequestBody($updatableSettings));
231231
}
232232
}
233233

@@ -298,4 +298,13 @@ private function prefixName(): string
298298

299299
return $indexConfiguration['prefix'] . '-' . $this->name;
300300
}
301+
302+
private function encodeRequestBody(array $content): string
303+
{
304+
if (empty($content)) {
305+
return '';
306+
}
307+
308+
return json_encode($content);
309+
}
301310
}

0 commit comments

Comments
 (0)