Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-php): send {} for an omitted list, accept a non-string error

Yudhi Armyndharisc534ff336324
diff --git a/sdk/php/src/Http/HttpExecutor.php b/sdk/php/src/Http/HttpExecutor.php
index 43d68d125..20ccf6d8f 100644
--- a/sdk/php/src/Http/HttpExecutor.php
+++ b/sdk/php/src/Http/HttpExecutor.php
@@ -246,11 +246,14 @@ class HttpExecutor
$reason = $response->getReasonPhrase();
$message = "OpenWA API {$status} {$reason} — {$method} {$path}: {$messageText}";
+ // An intermediary that rewrites the body may send a non-string `error`; the kind stays unset then.
+ $errorKind = is_string($envelope['error'] ?? null) ? $envelope['error'] : null;
+
return OpenWAApiException::classify(
$status,
$message,
$data,
- $envelope['error'] ?? null,
+ $errorKind,
$response->getHeaders(),
);
}
diff --git a/sdk/php/src/Resources/GroupsResource.php b/sdk/php/src/Resources/GroupsResource.php
index d0024a9c8..bd14ffc36 100644
--- a/sdk/php/src/Resources/GroupsResource.php
+++ b/sdk/php/src/Resources/GroupsResource.php
@@ -201,7 +201,8 @@ class GroupsResource
*/
public function approveMembershipRequests(string $sessionId, string $groupId, ?array $participants = null): array
{
- $body = $participants === null ? [] : ['participants' => $participants];
+ // An omitted list is an empty object on the wire, as the other SDKs send, not an empty array.
+ $body = $participants === null ? new \stdClass() : ['participants' => $participants];
return $this->http->request('POST', "/api/sessions/{$this->http->encodeSegment($sessionId)}/groups/{$this->http->encodeSegment($groupId)}/membership-requests/approve", [], $body);
}
@@ -214,7 +215,8 @@ class GroupsResource
*/
public function rejectMembershipRequests(string $sessionId, string $groupId, ?array $participants = null): array
{
- $body = $participants === null ? [] : ['participants' => $participants];
+ // An omitted list is an empty object on the wire, as the other SDKs send, not an empty array.
+ $body = $participants === null ? new \stdClass() : ['participants' => $participants];
return $this->http->request('POST', "/api/sessions/{$this->http->encodeSegment($sessionId)}/groups/{$this->http->encodeSegment($groupId)}/membership-requests/reject", [], $body);
}
diff --git a/sdk/php/tests/ClientTest.php b/sdk/php/tests/ClientTest.php
index e3d4a931f..1e2dcc7cf 100644
--- a/sdk/php/tests/ClientTest.php
+++ b/sdk/php/tests/ClientTest.php
@@ -219,6 +219,23 @@ class ClientTest extends TestCase
}
}
+ public function testAnErrorKindThatIsNotAStringDoesNotBreakTheMapping(): void
+ {
+ $backend = (new MockBackend())->on(502, [
+ 'statusCode' => 502,
+ 'message' => 'upstream failed',
+ 'error' => ['detail' => 'x'],
+ ]);
+ try {
+ $backend->makeClient()->sessions->get('x');
+ $this->fail('Expected exception');
+ } catch (OpenWAApiException $e) {
+ $this->assertSame(502, $e->getStatus());
+ $this->assertNull($e->getErrorKind());
+ $this->assertStringContainsString('upstream failed', $e->getMessage());
+ }
+ }
+
public function testErrorCarriesStatusAndBody(): void
{
$backend = (new MockBackend())->on(404, [
diff --git a/sdk/php/tests/ResourcesTest.php b/sdk/php/tests/ResourcesTest.php
index 617297ff9..73e90690b 100644
--- a/sdk/php/tests/ResourcesTest.php
+++ b/sdk/php/tests/ResourcesTest.php
@@ -558,6 +558,8 @@ class ResourcesTest extends TestCase
$client->groups->rejectMembershipRequests('s', '[email protected]');
$this->assertStringEndsWith('/membership-requests/reject', $backend->lastCall()['url']);
$this->assertSame([], $backend->lastCall()['body']);
+ // On the wire that is an empty object, as in the other SDKs, not an empty array.
+ $this->assertSame('{}', $backend->rawBody(2));
}
public function testCreateCallLink(): void