Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-php): send every object-map field as a JSON object

Yudhi Armyndharisfb859c66c73e
diff --git a/sdk/php/src/Resources/MessagesResource.php b/sdk/php/src/Resources/MessagesResource.php
index 37bac33ab..07fcc66c0 100644
--- a/sdk/php/src/Resources/MessagesResource.php
+++ b/sdk/php/src/Resources/MessagesResource.php
@@ -98,10 +98,9 @@ class MessagesResource
/** @return array<string,mixed> */
public function sendTemplate(string $sessionId, array $body, ?string $idempotencyKey = null): 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();
+ // Encode maps as objects without assigning through caller-held references.
+ if (isset($body['vars']) && is_array($body['vars'])) {
+ $body = array_merge($body, ['vars' => (object) $body['vars']]);
}
return $this->http->request('POST', "/api/sessions/{$this->http->encodeSegment($sessionId)}/messages/send-template", [], $body, $idempotencyKey);
}
@@ -243,12 +242,19 @@ 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();
- }
+ // Rebuild each array layer so referenced maps, items and lists stay untouched.
+ if (isset($body['messages']) && is_array($body['messages'])) {
+ $body = array_merge($body, [
+ 'messages' => array_map(static function ($item) {
+ if (is_array($item) && isset($item['variables']) && is_array($item['variables'])) {
+ return array_merge($item, ['variables' => (object) $item['variables']]);
+ }
+ return $item;
+ }, $body['messages']),
+ ]);
+ }
+ if (isset($body['options']) && is_array($body['options'])) {
+ $body = array_merge($body, ['options' => (object) $body['options']]);
}
return $this->http->request('POST', "/api/sessions/{$this->http->encodeSegment($sessionId)}/messages/send-bulk", [], $body);
}
diff --git a/sdk/php/src/Resources/SessionsResource.php b/sdk/php/src/Resources/SessionsResource.php
index d68f9f09b..96f27b3d1 100644
--- a/sdk/php/src/Resources/SessionsResource.php
+++ b/sdk/php/src/Resources/SessionsResource.php
@@ -90,10 +90,9 @@ class SessionsResource
*/
public function create(array $body): array
{
- // config 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['config']) && $body['config'] === []) {
- $body['config'] = new \stdClass();
+ // Preserve object-map shape without writing through caller-held references.
+ if (isset($body['config']) && is_array($body['config'])) {
+ $body = array_merge($body, ['config' => (object) $body['config']]);
}
return $this->http->request('POST', '/api/sessions', [], $body);
}
diff --git a/sdk/php/src/Resources/WebhooksResource.php b/sdk/php/src/Resources/WebhooksResource.php
index 6ef81c3e0..084a4d3b1 100644
--- a/sdk/php/src/Resources/WebhooksResource.php
+++ b/sdk/php/src/Resources/WebhooksResource.php
@@ -94,10 +94,9 @@ 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();
+ // Preserve object-map shape without writing through caller-held references.
+ if (isset($body['headers']) && is_array($body['headers'])) {
+ $body = array_merge($body, ['headers' => (object) $body['headers']]);
}
return $this->http->request('POST', "/api/sessions/{$this->http->encodeSegment($sessionId)}/webhooks", [], $body);
}
@@ -112,9 +111,8 @@ 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();
+ if (isset($body['headers']) && is_array($body['headers'])) {
+ $body = array_merge($body, ['headers' => (object) $body['headers']]);
}
return $this->http->request('PUT', "/api/sessions/{$this->http->encodeSegment($sessionId)}/webhooks/{$this->http->encodeSegment($id)}", [], $body);
}
diff --git a/sdk/php/tests/ResourcesTest.php b/sdk/php/tests/ResourcesTest.php
index 59bbe256b..6f2898019 100644
--- a/sdk/php/tests/ResourcesTest.php
+++ b/sdk/php/tests/ResourcesTest.php
@@ -268,6 +268,129 @@ class ResourcesTest extends TestCase
$this->assertSame('{"name":"n","config":{}}', $backend->rawBody(3));
}
+ /** @dataProvider objectMaps */
+ public function testMapFieldsStayObjectsForEveryArrayKeyShape(array $map): void
+ {
+ $backend = new MockBackend();
+ foreach (range(1, 5) as $ignored) {
+ $backend->on(200, []);
+ }
+ $client = $backend->makeClient();
+ $template = ['chatId' => 'c', 'templateName' => 't', 'vars' => $map];
+ $bulk = ['messages' => [['chatId' => 'c', 'type' => 'text', 'content' => ['text' => '{{0}}'], 'variables' => $map]], 'options' => []];
+ $webhook = ['url' => 'https://receiver.example/hook', 'events' => ['message.received'], 'headers' => $map];
+ $session = ['name' => 'fixture-session', 'config' => $map];
+ $original = serialize([$template, $bulk, $webhook, $session]);
+
+ $client->messages->sendTemplate('s', $template, 'template-key');
+ $client->messages->sendBulk('s', $bulk);
+ $client->webhooks->create('s', $webhook);
+ $client->webhooks->update('s', 'w', ['headers' => $map]);
+ $client->sessions->create($session);
+
+ $bodies = array_map(fn ($index) => json_decode($backend->rawBody($index), false, 512, JSON_THROW_ON_ERROR), range(0, 4));
+ foreach ([$bodies[0]->vars, $bodies[1]->messages[0]->variables, $bodies[2]->headers, $bodies[3]->headers, $bodies[4]->config] as $value) {
+ $this->assertInstanceOf(\stdClass::class, $value);
+ $this->assertSame($map, (array) $value);
+ }
+ $this->assertInstanceOf(\stdClass::class, $bodies[1]->options);
+ $this->assertSame([], (array) $bodies[1]->options);
+ $this->assertIsArray($bodies[1]->messages);
+ $this->assertSame($original, serialize([$template, $bulk, $webhook, $session]));
+ $this->assertSame('template-key', $backend->callAt(0)['headers']['idempotency-key']);
+ $this->assertArrayNotHasKey('idempotency-key', $backend->callAt(1)['headers']);
+ }
+
+ public static function objectMaps(): array
+ {
+ return [
+ 'numeric zero' => [[0 => 'one']],
+ 'contiguous numeric keys' => [[0 => 'one', 1 => 'two']],
+ 'sparse numeric keys' => [[1 => 'one']],
+ 'named keys' => [['custom' => 'value']],
+ 'empty map' => [[]],
+ ];
+ }
+
+ /** @dataProvider referencedMaps */
+ public function testMapNormalizationPreservesCallerReferences(string $kind): void
+ {
+ $map = $kind === 'bulk-options' ? ['delayBetweenMessages' => 3000] : ['custom' => 'Alice'];
+ $item = ['chatId' => 'c', 'type' => 'text', 'content' => ['text' => '{{custom}}'], 'variables' => $map];
+ $items = [$item];
+ $body = match ($kind) {
+ 'template' => ['chatId' => 'c', 'templateName' => 't', 'vars' => &$map],
+ 'bulk-variable' => ['messages' => [['chatId' => 'c', 'type' => 'text', 'content' => ['text' => '{{custom}}'], 'variables' => &$map]]],
+ 'bulk-item' => ['messages' => [&$item]],
+ 'bulk-messages' => ['messages' => &$items],
+ 'bulk-options' => ['messages' => [$item], 'options' => &$map],
+ 'webhook-create', 'webhook-update' => ['headers' => &$map],
+ 'session' => ['name' => 'fixture-session', 'config' => &$map],
+ };
+ $before = serialize([$body, $map, $item, $items]);
+ $backend = (new MockBackend())->on(200, []);
+ $client = $backend->makeClient();
+
+ match ($kind) {
+ 'template' => $client->messages->sendTemplate('s', $body),
+ 'bulk-variable', 'bulk-item', 'bulk-messages', 'bulk-options' => $client->messages->sendBulk('s', $body),
+ 'webhook-create' => $client->webhooks->create('s', $body),
+ 'webhook-update' => $client->webhooks->update('s', 'w', $body),
+ 'session' => $client->sessions->create($body),
+ };
+
+ $this->assertSame($before, serialize([$body, $map, $item, $items]));
+ $sent = json_decode($backend->rawBody(0), false, 512, JSON_THROW_ON_ERROR);
+ $value = match ($kind) {
+ 'template' => $sent->vars,
+ 'bulk-variable', 'bulk-item', 'bulk-messages' => $sent->messages[0]->variables,
+ 'bulk-options' => $sent->options,
+ 'webhook-create', 'webhook-update' => $sent->headers,
+ 'session' => $sent->config,
+ };
+ $this->assertInstanceOf(\stdClass::class, $value);
+ $this->assertSame($map, (array) $value);
+ }
+
+ public static function referencedMaps(): array
+ {
+ return [
+ 'template variables' => ['template'],
+ 'bulk variables' => ['bulk-variable'],
+ 'bulk item' => ['bulk-item'],
+ 'bulk messages' => ['bulk-messages'],
+ 'bulk options' => ['bulk-options'],
+ 'webhook create' => ['webhook-create'],
+ 'webhook update' => ['webhook-update'],
+ 'session config' => ['session'],
+ ];
+ }
+
+ public function testMapNormalizationPreservesNullOmissionAndProtocolLists(): void
+ {
+ $backend = new MockBackend();
+ foreach (range(1, 5) as $ignored) {
+ $backend->on(200, []);
+ }
+ $client = $backend->makeClient();
+ $client->messages->sendTemplate('s', ['chatId' => 'c', 'templateName' => 't', 'vars' => null]);
+ $client->messages->sendTemplate('s', ['chatId' => 'c', 'templateName' => 't']);
+ $client->messages->sendPoll('s', ['chatId' => 'c', 'name' => 'Q', 'options' => ['A', 'B'], 'mentions' => []]);
+ $client->webhooks->create('s', ['url' => 'https://receiver.example/hook', 'events' => ['message.received'], 'headers' => null, 'filters' => ['conditions' => []]]);
+ $client->request('POST', '/api/anything', [], ['first', 'second']);
+
+ $bodies = array_map(fn ($index) => json_decode($backend->rawBody($index), false, 512, JSON_THROW_ON_ERROR), range(0, 4));
+ $this->assertTrue(property_exists($bodies[0], 'vars'));
+ $this->assertNull($bodies[0]->vars);
+ $this->assertFalse(property_exists($bodies[1], 'vars'));
+ $this->assertSame(['A', 'B'], $bodies[2]->options);
+ $this->assertSame([], $bodies[2]->mentions);
+ $this->assertNull($bodies[3]->headers);
+ $this->assertSame(['message.received'], $bodies[3]->events);
+ $this->assertSame([], $bodies[3]->filters->conditions);
+ $this->assertSame(['first', 'second'], $bodies[4]);
+ }
+
public function testBulkItemsEncodeEmptyVariablesAsJsonObject(): void
{
$backend = (new MockBackend())->on(200, ['id' => 'b1']);