Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk): encode empty map fields as JSON objects in the PHP client

Yudhi Armyndharise79bea982793
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2c46339f9..685c8a119 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- PHP SDK: an empty `headers` map (webhook create/update), an empty `vars` map (send-template), and empty per-item `variables` (send-bulk) encode as JSON `{}`; they serialized as `[]`, which the gateway rejects for map-typed fields.
- Both bundled Compose files forward every documented runtime knob; `WEBHOOK_SSRF_REDIRECTS`, `PLUGIN_INSTALL_REQUIRE_PIN` and ~75 other `.env` settings were unreachable in the container. A spec now derives the required list from `.env.example`.
- Ingress delivery ids BullMQ refuses at enqueue (numeric, `redrive:<uuid>`, `0:`-led) are hashed to a legal job id, namespaced by plugin/instance; the old refusal read as a Redis failure and silently degraded the delivery to inline dispatch with no retry.
- One precise unique-violation predicate (`23505`, `SQLITE_CONSTRAINT_UNIQUE`/`_PRIMARYKEY`): the old prefix match treated every SQLite constraint failure (FK/NOT NULL/CHECK) as a duplicate, swallowing genuine persistence failures and answering misleading 409s.
diff --git a/sdk/php/src/Resources/MessagesResource.php b/sdk/php/src/Resources/MessagesResource.php
index 58e0d6ac6..329caef96 100644
--- a/sdk/php/src/Resources/MessagesResource.php
+++ b/sdk/php/src/Resources/MessagesResource.php
@@ -98,6 +98,11 @@ class MessagesResource
/** @return array<string,mixed> */
public function sendTemplate(string $sessionId, array $body): array
{
+ // vars is a map: an empty PHP array would serialize as a JSON list [] and be rejected by the
+ // gateway's object validation. Cast the empty map to stdClass so it encodes as {}.
+ if (isset($body['vars']) && $body['vars'] === []) {
+ $body['vars'] = new \stdClass();
+ }
return $this->http->request('POST', "/api/sessions/{$this->http->encodeSegment($sessionId)}/messages/send-template", [], $body);
}
@@ -221,6 +226,13 @@ class MessagesResource
/** @return array<string,mixed> */
public function sendBulk(string $sessionId, array $body): array
{
+ // variables inside each item is a map: an empty PHP array would serialize as a JSON list []
+ // and be rejected by the gateway's object validation, like headers/vars on the other sends.
+ foreach ($body['messages'] ?? [] as $i => $item) {
+ if (is_array($item) && isset($item['variables']) && $item['variables'] === []) {
+ $body['messages'][$i]['variables'] = new \stdClass();
+ }
+ }
return $this->http->request('POST', "/api/sessions/{$this->http->encodeSegment($sessionId)}/messages/send-bulk", [], $body);
}
diff --git a/sdk/php/src/Resources/WebhooksResource.php b/sdk/php/src/Resources/WebhooksResource.php
index 369ae9994..561d1833b 100644
--- a/sdk/php/src/Resources/WebhooksResource.php
+++ b/sdk/php/src/Resources/WebhooksResource.php
@@ -64,6 +64,11 @@ class WebhooksResource
*/
public function create(string $sessionId, array $body): array
{
+ // headers is a map: an empty PHP array would serialize as a JSON list [] and be rejected by
+ // the gateway's object validation. Cast the empty map to stdClass so it encodes as {}.
+ if (isset($body['headers']) && $body['headers'] === []) {
+ $body['headers'] = new \stdClass();
+ }
return $this->http->request('POST', "/api/sessions/{$this->http->encodeSegment($sessionId)}/webhooks", [], $body);
}
@@ -73,6 +78,10 @@ class WebhooksResource
*/
public function update(string $sessionId, string $id, array $body): array
{
+ // Same empty-map cast as create(): headers must encode as {} when empty.
+ if (isset($body['headers']) && $body['headers'] === []) {
+ $body['headers'] = new \stdClass();
+ }
return $this->http->request('PUT', "/api/sessions/{$this->http->encodeSegment($sessionId)}/webhooks/{$this->http->encodeSegment($id)}", [], $body);
}
diff --git a/sdk/php/tests/MockBackend.php b/sdk/php/tests/MockBackend.php
index 767a72587..fc6bbfc62 100644
--- a/sdk/php/tests/MockBackend.php
+++ b/sdk/php/tests/MockBackend.php
@@ -81,6 +81,20 @@ class MockBackend
]);
}
+ /**
+ * The raw request body string, for assertions that must distinguish JSON {} from []
+ * (callAt() decodes associatively, which collapses both to an empty PHP array).
+ *
+ * @return string
+ */
+ public function rawBody(int $index): string
+ {
+ if (!isset($this->recorded[$index])) {
+ return '';
+ }
+ return (string) $this->recorded[$index]->getBody();
+ }
+
/** @return array<string,mixed> */
public function lastCall(): array
{
diff --git a/sdk/php/tests/ResourcesTest.php b/sdk/php/tests/ResourcesTest.php
index afdd3d4f0..2afe5ea5d 100644
--- a/sdk/php/tests/ResourcesTest.php
+++ b/sdk/php/tests/ResourcesTest.php
@@ -223,6 +223,41 @@ class ResourcesTest extends TestCase
$this->assertStringContainsString('/webhooks/w1/test', $backend->calls()[5]['url']);
}
+ public function testEmptyMapFieldsEncodeAsJsonObjectNotJsonList(): void
+ {
+ // headers (webhook create/update) and vars (send-template) are map-typed on the wire; an
+ // empty PHP array would serialize as a JSON list [] and the gateway's object validation
+ // rejects it. The SDK must cast the empty map so it encodes as {}.
+ $backend = (new MockBackend())->on(201, ['id' => 'w1']);
+ $client = $backend->makeClient();
+
+ $client->webhooks->create('s', ['url' => 'u', 'events' => ['*'], 'headers' => []]);
+ $this->assertSame('{"url":"u","events":["*"],"headers":{}}', $backend->rawBody(0));
+
+ $backend->on(200, ['id' => 'w1']);
+ $client->webhooks->update('s', 'w1', ['active' => true, 'headers' => []]);
+ $this->assertSame('{"active":true,"headers":{}}', $backend->rawBody(1));
+
+ $backend->on(200, ['id' => 'm1']);
+ $client->messages->sendTemplate('s', ['chatId' => 'c', 'templateName' => 't', 'vars' => []]);
+ $this->assertSame('{"chatId":"c","templateName":"t","vars":{}}', $backend->rawBody(2));
+ }
+
+ public function testBulkItemsEncodeEmptyVariablesAsJsonObject(): void
+ {
+ $backend = (new MockBackend())->on(200, ['id' => 'b1']);
+ $client = $backend->makeClient();
+
+ $client->messages->sendBulk('s', ['messages' => [
+ ['chatId' => 'c1', 'type' => 'template', 'templateName' => 't', 'variables' => []],
+ ['chatId' => 'c2', 'type' => 'template', 'templateName' => 't', 'variables' => ['x' => 'y']],
+ ]]);
+ $this->assertSame(
+ '{"messages":[{"chatId":"c1","type":"template","templateName":"t","variables":{}},{"chatId":"c2","type":"template","templateName":"t","variables":{"x":"y"}}]}',
+ $backend->rawBody(0)
+ );
+ }
+
public function testWebhookCreateForwardsPolymorphicFilterValuesVerbatim(): void
{
$backend = (new MockBackend())->on(201, ['id' => 'w1']);